leetcode41. First Missing Positive

Posted weidiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode41. First Missing Positive相关的知识,希望对你有一定的参考价值。

给定一个数组,找出数组中不曾出现的最小正整数。

关键在于需要对原数组进行操作。

class Solution:
    def firstMissingPositive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums: return 1
        for i in range(len(nums)):
            while 0 < nums[i] < len(nums) and nums[nums[i] - 1] != nums[i]:
                temp = nums[i]
                nums[i] = nums[temp - 1]
                nums[temp - 1] = temp
        print(nums)
        for i in range(len(nums)):
            if nums[i] != i + 1:
                return i + 1
        return len(nums)+1

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

LeetCode OJ 41First Missing Positive

leetcode:41. First Missing Positive (Java)

[leetcode-41-First Missing Positive]

Leetcode 41: First Missing Positive

LeetCode41 First Missing Positive

leetcode 41. First Missing Positive