Skip to content

Instantly share code, notes, and snippets.

@b27lu
Created March 12, 2014 16:31
Show Gist options
  • Save b27lu/9510728 to your computer and use it in GitHub Desktop.
Save b27lu/9510728 to your computer and use it in GitHub Desktop.
First Missing Positive at LeetCode
public class Solution {
public int firstMissingPositive(int[] A) {
int n = A.length;
int i = 0;
while(i<n){
if(A[i] > 0 && A[i] <= n && A[i]-1 != i && A[A[i]-1] != A[i]){
int temp = A[A[i]-1];
A[A[i]-1] = A[i];
A[i] = temp;
}
else
i++;
}
for(i = 0;i<n;i++){
if(A[i] != i+1)
return i+1;
}
return n+1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment