Question: How to find the first integer greater than 0 and not in the unordered integer array. For example, [100, 0] returns 3, [,-], returns 2, [1, 5, 3, 4, 2], returns 6, [, 3, 2, 1, 6, 8, 5] returns 4. Requires O (1) space and O (n) Time
A: keep a [I] = I.
# Include <stdio. h> void swap (Int & A, Int & B) {int c = A; A = B; B = C;} int findfirstnumbernotexistenceinarray (int A [], int N) {int I; for (I = 0; I <n; I ++) while (A [I]> 0 & A [I] <= N & A [I]! = I + 1 & A [I]! = A [A [I]-1]) Swap (A [I], a [A [I]-1]); for (I = 0; I <N; I ++) if (a [I]! = I + 1) break; return I + 1;} void printfarray (int A [], int N) {for (INT I = 0; I <n; I ++) printf ("% d", a [I]); putchar ('\ n');} int main () {const int maxn = 5; int A [maxn] = {2,-100, 4, 1, 70}; printfarray (A, maxn ); printf ("the missing number of this array is % d \ n", findfirstnumbernotexistenceinarray (A, maxn); Return 0 ;}
Reference: http://blog.csdn.net/morewindows/article/details/12683723
Returns the smallest integer.