1. 程式人生 > >leetcode41. First Missing Positive

leetcode41. First Missing Positive

int markdown python pri cnblogs 最小 正整數 leetcode first

給定一個數組,找出數組中不曾出現的最小正整數。

關鍵在於需要對原數組進行操作。

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