Python 2.7 O(n) solution using exception handling


  • -1
    I
    class Solution(object):
        def firstMissingPositive(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            found=False
            i=1
            while(not found):
                try:
                    print nums.remove(i)
                    i=i+1
                except ValueError:
                    found = True
                    return i
    

  • 1
    L

    removal in lists is O(n), meaning this solution is in O(n^2)


Log in to reply
 

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