Simple Python Solution


  • -1
    K

    Use an array to store whether an positive integer has appeared or not.

    class Solution(object):
        def firstMissingPositive(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            l = [0] * (len(nums) + 1)
            for i in nums:
                if i > 0 and i < len(nums)+1:
                    l[i] = 1
            for i in range(1,len(nums)+1):
                if l[i] == 0:
                    return i
            return len(nums)+1
    

  • 0
    X

    You are not allowed to use an array because the space complexity has to be constant


Log in to reply
 

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