First Missing Positive

题目: 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
posted @ 2018-03-16 22:52  Ping的博客  阅读(75)  评论(0编辑  收藏  举报