268. Missing Number; 41. First Missing Positive

268. Missing Number
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n=len(nums)
return sum(range(n+1))-sum(nums)


41. First Missing Positive
class Solution(object):
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
first_positiove = 1
flag=True
if nums<>[]:
if max(nums)>1:
full_nums=range(1,max(nums)+1)
for i in full_nums:
if i > 0 and i not in nums:
first_positiove = i
flag = False
break
if flag:
first_positiove=max(nums)+1
elif max(nums)==1:
first_positiove =2
return first_positiove

猜你喜欢

转载自www.cnblogs.com/ffeng0312/p/9236791.html