Python solution - is this constant space?


  • 0
    A
    class Solution(object):
        def firstMissingPositive(self, nums):
            nums = filter(lambda x: x > 0, nums)
            for i in xrange(len(nums)):
                j = abs(nums[i])-1
                if j >= 0 and j < len(nums) and nums[j] > 0:
                    nums[j] = -nums[j]
            for i in xrange(len(nums)):
                if nums[i] > 0:
                    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.