Python solution


  • 0
    H
    class Solution:
        # @param A, a list of integers
        # @return an integer
        def firstMissingPositive(self, A):
            if not A:
                return 1
            
            n = len(A)
            for i in range(n):
                while 0 < A[i] <= n and i + 1 != A[i] and A[i] != A[A[i] -1]:
                    A[A[i] - 1], A[i] = A[i], A[A[i] - 1]
                    
            for i in range(n):
                if i + 1 != A[i]:
                    return i + 1
            
            return len(A) + 1

Log in to reply
 

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