Concise python solution


  • 0
    Y
    class Solution:
        # @param {integer[]} nums
        # @return {integer}
        def firstMissingPositive(self, nums):
            if not nums:
                return 1
    
            for i in range(0, len(nums)):
                j = i
                while nums[j] > 0 and nums[j] <= len(nums) and nums[j] != j + 1 and nums[j] != nums[nums[j] - 1]:
                    t = nums[j] - 1
                    nums[j], nums[t] = nums[t], nums[j]
    
            for i in range(0, len(nums)):
                if nums[i] != i + 1:
                    return i + 1
    
            return len(nums) + 1

  • 0
    W

    I get confused. I use "if" where you use "while" and get wrong number


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.