forked from super30admin/Array-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem 1.py
28 lines (23 loc) · 868 Bytes
/
Problem 1.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
#time complexity = O(n)
#space complexity = O(n)
class Solution(object):
def findDisappearedNumbers(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
# Hash table for keeping track of the numbers in the array
# Note that we can also use a set here since we are not
# really concerned with the frequency of numbers.
hash_table = {}
# Add each of the numbers to the hash table
for num in nums:
hash_table[num] = 1
# Response array that would contain the missing numbers
result = []
# Iterate over the numbers from 1 to N and add all those
# that don't appear in the hash table.
for num in range(1, len(nums) + 1):
if num not in hash_table:
result.append(num)
return result