448. Find All Numbers Disappeared in an Array
Solution
-
Since the array contians number in range [1, n], we can use their value as index to track the information.
-
When a number is visited, we mark the value it is point to negative.
-
The positive number’s index disappeared in the array.
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
for i in xrange(len(nums)):
index = abs(nums[i]) - 1
nums[index] = - abs(nums[index])
return [i + 1 for i in range(len(nums)) if nums[i] > 0]
PREVIOUSSingle Number