1. 程式人生 > >First Missing Positive

First Missing Positive

you pos missing body OS range amp num markdown

題目: Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space.

方案:

class Solution:
 def firstMissingPositive(self, nums):
     """
     :type nums: List[int]
     :rtype: int
     """
     if nums == []:
         return 1
     M = max(nums)
     for i in range(1,M):
         if i not in nums:
             return i
     return M + 1

First Missing Positive