Thursday, September 18, 2014

Leetcode: First Missing Positive @Python

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

No comments :

Post a Comment