forked from super30admin/Array-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_dissappearence.py
40 lines (30 loc) · 939 Bytes
/
find_dissappearence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Approach recursively mark positions of array elements in place.
Iterate over array again and return indices where elements != -1
Time complexity O(n)
Space complexity O(1)
"""
class Solution(object):
def place(self, element, nums) :
if element == -1 :
return
nxt = nums[element -1 ]
if nxt == -1 :
# cannot place element
return
nums[element -1 ] = -1 # mark elemnt-1 index as visited
return self.place(nxt,nums)
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
self.missing = []
self.nums = nums
for i in nums :
self.place(i, nums)
missing = []
for i in range(len(nums)):
if nums[i] != -1 :
missing.append(i+1)
return missing