Python: a easy-understanding way to do this


  • -2
    T
    def firstMissingPositive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        copy_nums = []
    
        for num in nums:
            if num > 0:
                copy_nums.append(num)
    
        if not copy_nums:
            return 1
    
        top = max(copy_nums)
        empty_stack = []
    
        for i in xrange(top):
            empty_stack.append("empty")
    
        for num in copy_nums:
            empty_stack[num-1] = num
    
        for pos,value in enumerate(empty_stack):
            if value == "empty":
                return pos+1
    
        return top+1

Log in to reply
 

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