First Missing Positive

Posted ping1994

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于First Missing Positive的主要内容,如果未能解决你的问题,请参考以下文章

刷题41. First Missing Positive

Leetcode 41. First Missing Positive

41. First Missing Positive *HARD*

leetCode题解之First Missing Positive

leetcode-First Missing Positive

First Missing Positive