https://oj.leetcode.com/problems/first-missing-positive/
http://blog.csdn.net/linhuanmars/article/details/20884585
Public class solution { public int firstmissingpositive (int[] a) { // if it can be sorted, sort after 1 to start checking // If a number is 3, replace it with a third space // i, continue to replace, Dead Loop! // If I-bit values < 0,i++ if (a == null | | a.length == 0) return 1; int i = 0; while (I < a.length) { if (A [i] <= 0 | | a[i] > a.length) { // Invalid number we don ' t care A[i] = -1; i++; continue; } if (a[i] == i + 1) & Nbsp; { // Aready good i ++; continue; } // No need to swap // Avoid dead loop // Definition J Convenient coding int j = A[i] - 1; if (A[j] == a[i]) { A[i] = -1; i ++; continue; } // swap A[i] and A[A[i] - 1] int t = A[i]; A[i] = A[j]; a[j] = t; } // Check first -1 for (i = 0 ; i < a.length ; i ++) { if (a[i] < 0) return i + 1; } return a.length + 1; }}
[leetcode]41 First Missing Positive