Created
March 12, 2014 16:31
-
-
Save b27lu/9510728 to your computer and use it in GitHub Desktop.
First Missing Positive at LeetCode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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