관리 메뉴

솜씨좋은장씨

[leetCode] 268. Missing Number (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 268. Missing Number (Python)

솜씨좋은장씨 2020. 6. 9. 23:13
728x90
반응형

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

 

Example 1:

Input: [3,0,1]
Output: 2

Example 2:

Input: [9,6,4,2,3,5,7,0,1]
Output: 8

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

 

Solution

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        max_num = max(nums)
        
        my_nums = set([num for num in range(max_num + 1)])
        
        nums = set(nums)
        
        differ = my_nums - nums
        
        if len(differ) == 0:
            answer = max_num + 1
        else:
            answer = list(differ)[0]
        
        return answer

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments