Counting sort.
Also here we need pay attention to this.
public int firstMissingPositive(int[] A) {
if (A==null || A.length == 0) return 1;
for (int i=0; i<A.length; i++) {
if (A[i]!=i+1 && A[i] <=A.length && A[i] > 0 && A[A[i]-1]!=A[i]) {
/*It wont work
int temp = A[i];
A[i] = A[A[i]-1];
A[A[i]-1] = temp;
*/
int temp = A[A[i]-1];
A[A[i]-1] = A[i];
A[i] = temp;
i--;
}
}
for (int i=0; i<A.length;i++) {
if (A[i] != i+1) {
return i+1;
}
}
return A.length+1;
}
No comments:
Post a Comment