python solution, not sure how good it is


  • 0
    C
        def firstMissingPositive(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n = len(nums)
            for i in range(0,n):
                if nums[i] < 1:
                    nums[i] = n + 1
            for i in range(0,n):
                x = abs(nums[i])
                if 0 < x <= n and nums[x - 1] > 0:
                    nums[x - 1] = -nums[x - 1]
            for i in range(0,n):
                if nums[i] > 0:
                    return i + 1
            return n + 1
    

Log in to reply
 

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