관리 메뉴

솜씨좋은장씨

[leetCode] 448. Find All Numbers Disappeared in an Array (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 448. Find All Numbers Disappeared in an Array (Python)

솜씨좋은장씨 2020. 8. 6. 22:08
728x90
반응형

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

 

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]

 

Solution

class Solution:
    def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
        if len(nums) < 0:
            answer = []
        else:
            max_num = len(nums)
            answer = list(set(range(1,max_num + 1)) - set(nums))

        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