-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0448.Find_All_Numbers_Disappeared_in_an_Array.py
More file actions
54 lines (39 loc) Β· 1.33 KB
/
0448.Find_All_Numbers_Disappeared_in_an_Array.py
File metadata and controls
54 lines (39 loc) Β· 1.33 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]
Example 2:
Input: nums = [1,1]
Output: [2]
Constraints:
n == nums.length
1 <= n <= 105
1 <= nums[i] <= n
Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
"""
# hashset O(n) O(n)
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
res = []
nums_set = set(nums)
for i in range(1, len(nums) + 1):
if i not in nums_set:
res.append(i)
return res
# if num not in nums o(n)
"""
We use the sign of the index as the indicator. If one number never occur,
we know the number corresponding to the idx will never be negative.
[4,3,1,3] -- > [-4,3,-1,-3], 2 is missing, so num[2-1] will never be changed to be negative
"""
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
for num in nums:
idx = abs(num) - 1
nums[idx] = -abs(nums[idx])
res =[]
for idx, num in enumerate(nums):
if num > 0:
res.append(idx + 1)
return res