-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing-number.py
More file actions
46 lines (36 loc) · 986 Bytes
/
missing-number.py
File metadata and controls
46 lines (36 loc) · 986 Bytes
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
# -*- coding: utf-8 -*-
"""
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
Note:
Your algorithm should run in linear runtime complexity.
Could you implement it using only constant extra space complexity?
"""
class Solution:
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
>>> test1 = [3, 0, 1]
>>> test2 = [9,6,4,2,3,5,7,0,1]
>>> test3 = [0]
>>> s = Solution()
>>> s.missingNumber(test1)
2
>>> s.missingNumber(test2)
8
>>> s.missingNumber(test3)
1
"""
l = len(nums)
current_value = sum(nums)
target_value = sum(i for i in range(l + 1))
return target_value - current_value
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)