First missing positive Python solution


  • 0
    E

    Probably not the shortest solution, but it works and covers edge cases
    """

    class Solution(object):
    def firstMissingPositive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 1
        for negN in sorted(nums):
            minN = min(nums)
            if minN <= 0:
                nums.pop(nums.index(minN))
            else:
                break
        if not nums:
            return 1
        
        for i in sorted(nums):
            if not 1 in nums:
                return 1
            elif i - 1 > 0 and i - 1 not in nums:
                return i - 1
            elif i + 1 not in nums:
                return i + 1
    

    """


Log in to reply
 

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