파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 10. 23. 23:45

https://leetcode.com/problems/set-mismatch/

 

Set Mismatch - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

생각보다 간단하게 수학적인 식으로 풀 수 있다.

class Solution:
    def findErrorNums(self, nums: List[int]) -> List[int]:        
        l = len(nums)
        s, a, b = l*(l+1) // 2, sum(nums), sum(set(nums))        
        
        return [a-b, s-b]
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.10.25
Today's Challenge  (0) 2022.10.24
Today's Challenge  (0) 2022.10.22
Today's Challenge  (0) 2022.10.21
Today's Challenge  (0) 2022.10.20
Comments