Missing Number

Missing Number

1
2
3
4
5
6
7
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return (set([x for x in xrange(len(nums) + 1)]) - set(nums)).pop()
1
2
3
4
5
6
7
8
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
return n * (n+1) / 2 - sum(nums)

test code

1
2
3
4
In [135]: s = Solution(); t = s.missingNumber([9,6,4,2,3,5,7,0,1])
In [136]: t
Out[136]: 8

leet code