classSolution:# @param A, a list of integers# @return an integerdeffirstMissingPositive(self,A):# write your code hereifA==Noneorlen(A)==0:return1length=len(A)# The first missing positive should in [1...N]# Scan A, if A[i] <= 0 or A[i] > N, we assign a dummy integer -1foriinrange(length):ifA[i]<=0orA[i]>length:A[i]=-1# Use Aray A as hash table, if A[i] in [1...N], we put A[i] to A[A[i]-1]# A[i] != A[A[i]-1] is used for removing dead loop if A = [1]foriinrange(length):whileA[i]!=-1andA[i]!=A[A[i]-1]:tmp=A[A[i]-1]A[A[i]-1]=A[i]A[i]=tmp# Scan the new A, find A[i] != i+1foriinrange(length):ifA[i]!=i+1:returni+1# for case: [1,2,3], missing is 4returnlength+1