a straightforward python implementation


  • -1
    X
    class Solution(object):
        def firstMissingPositive(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if nums:
                s = []
                maximum = max(nums)
                minimum = min(nums)
                i = minimum
                while i<=maximum:
                    s.append(i)
                    i += 1
                l = len(s)
                if minimum>=2:
                    return 1
                elif minimum<2:
                    for j in range(l):
                        if s[j]>0 and s[j] not in nums:
                            return s[j]
                            break
                        else:
                            continue
                return (maximum+1)
            else:
                return 1
    

  • 0
    X

    your space complexity is not constant but worst case linear


Log in to reply
 

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