forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_swap_n.cpp
41 lines (34 loc) · 831 Bytes
/
AC_swap_n.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_swap_n.cpp
* Create Date: 2015-01-28 09:55:08
* Descripton: Just like bucket sort.
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
int firstMissingPositive(int A[], int n) {
for (int i = 0; i < n; ) {
if ((A[i] < n && A[i] > 0) &&
(A[i] != i && A[A[i]] != A[i]))
swap(A[i], A[A[i]]);
else
++i;
}
for (int i = 1; i < n; ++i)
if (!A[i] != i)
return i;
return A[0] == n ? n + 1 : n;
}
};
int main() {
int n, A[100];
Solution s;
cin >> n;
for (int i = 0; i < n; i++)
cin >> A[i];
cout << s.firstMissingPositive(A, n) << endl;
return 0;
}