public class Solution { public int firstMissingPositive(int[] A) { // Start typing your Java solution below // DO NOT write main() function int len = A.length; for(int i = 0; i <len; i++){ while(A[i] != i+1){ /* A[i] == A[A[i]-1]是为了检测当前值是否和前一个值相等 * eg: A[2] =3 && A[3] =3, 不加将进入死循环 */ if(A[i] > len || A[i] <=0 || A[i] == A[A[i]-1]){ break; } int temp = A[i]; A[i] = A[temp -1]; A[temp-1] = temp; } } for(int i = 0; i < len; i++){ if(A[i] != i+1) return i+1; } return len+1; } }
[Leetcode]--First Missing Positive
原文:http://www.cnblogs.com/RazerLu/p/3531944.html