Find All Numbers Disappeared in an Array

Find All Numbers Disappeared in an Array

One line Version - 152ms

1
2
3
4
5
6
7
class Solution:
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
return list(set(range(1, len(nums) + 1)) - set(nums))

Without Extra Space Version - 224ms

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
for i in range(len(nums)):
idx = abs(nums[i]) - 1
nums[idx] = - abs(nums[idx])
#print(i, idx, nums)
return [i + 1 for i in range(len(nums)) if nums[i] > 0]

test code

1
2
In [31]: s = Solution(); t = s.findDisappearedNumbers([4,3,2,7,8,2,3,1]); print(t)
[5, 6]

leet code