lintcode:find the missing number

Given an array contains N numbers of 0 .. N, find which number doesn’t exist in the array.

Example :
Given N = 3 and the array [0, 1, 3], return 2.

first solution

1
2
3
4
5
6
7
class :
# @return: an integer
def findMissing(self, nums):
# write your code here
new_list = range(len(nums)+1)
return (set(new_list)^set(nums)).pop()

set集合不能通过索引进行查找对象,只能通过pop

九章算法

1
2
3
4
5
6
7
8
9
class :
# @return: an integer
def findMissing(self, nums):
s = set(nums)
for i in range(0, len(nums) + 1):
if i not in s:
return i
return -1