forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC_array_n.cpp
More file actions
36 lines (31 loc) · 765 Bytes
/
AC_array_n.cpp
File metadata and controls
36 lines (31 loc) · 765 Bytes
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_array_n.cpp
* Create Date: 2015-01-28 09:38:55
* Descripton: Brute force. This will use O(n) space && O(n) time.
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
int firstMissingPositive(int A[], int n) {
vector<bool> rec(n + 2);
for (int i = 0; i < n; i++) {
if (A[i] < n + 2 && A[i] > 0)
rec[A[i]] = true;
}
for (int i = 1; i < n + 2; i++)
if (!rec[i])
return i;
}
};
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;
}