-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
30 lines (25 loc) · 839 Bytes
/
answer.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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
# O(n) Space Complexity
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
numbers = [0]*(len(nums)+1)
for n in nums:
numbers[n] = 1
for i in range(len(numbers)):
if numbers[i] == 0:
return i
#-------------------------------------------------------------------------------
# O(1) Space Complexity using math
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return (len(nums) * (len(nums) + 1) // 2) - sum(nums)
#-------------------------------------------------------------------------------