[
array
hash table
]
Leetcode 0448 Find All Numbers Disappeared in an Array
Problem statement
https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
Solution
See similar Problems 0041: First Missing positive and 0442: Find All Duplicates in an Array. We an either swap elements, or use trick where we add n
to all values of already found element. and then return indexes of elements which are <= n
.
Complexity
Time complexity is O(n)
, additional space is O(1)
.
Code
class Solution:
def findDisappearedNumbers(self, nums):
n = len(nums)
for num in nums:
nums[(num-1) % n] += n
return [i+1 for i in range(n) if nums[i] <= n]