Simple Python Sol


  • 0
    A
    class Solution(object):
        def firstMissingPositive(self, nums):
            #if len(nums) == 0:  return 1
            def swap(i, j):
                tmp = nums[i]
                nums[i] = nums[j]
                nums[j] = tmp
                
            for i in xrange(len(nums)):
                while 0 < nums[i] < len(nums) and nums[i] != i+1 and nums[nums[i]-1] != nums[i]:
                    swap(i, nums[i]-1)
                    
            
            for i in xrange(len(nums)):
                if nums[i] != i+1:
                    return i+1
            return len(nums)+1
    

Log in to reply
 

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