-
Notifications
You must be signed in to change notification settings - Fork 0
/
firstMissingPositive_MID.cc
65 lines (54 loc) · 1.4 KB
/
firstMissingPositive_MID.cc
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<math>
using namespace std;
class Solution {
public:
int firstMissingPositive(int A[], int n) {
if (!n) return 1;
for (int i = 0; i < n; i++) {
if (A[i] <= 0)
A[i] = n + 1;
}
for (int i = 0; i < n; i++) {
int index = abs(A[i]);
if (index <= n) {
if (A[index - 1] > 0)
A[index - 1] = -A[index - 1];
}
}
int i = 0;
while (i < n) {
if (A[i] > 0) break;
i++;
}
return i + 1;
}
//-----------------------------------//
int firstMissingPositive(int A[], int n) {
if (n == 0)
return 1;
for (int i = 0; i < n; ++i) {
if (A[i] <= 0)
A[i] = n + 1;
}
for (int i = 0; i < n; ++i) {
int index = abs(A[i]);
if (index >= 1 && index <= n && A[index - 1] > 0)// in case duplicated number, reverse even time will occur problem
A[index - 1] = -A[index - 1]; // mark all the location that its according number has shown up in somewhere in this array.
}
/* can also like this
for (int i = 0; i < n; i++) {
int tmp = abs(A[i]);
if (tmp <= n) {
A[tmp - 1] = -abs(A[tmp - 1]);
}
}*/
for (int i = 0; i < n; ++i) {
if (A[i] > 0)
return i + 1;
}
return n + 1;
}
};
int main(int argc, char** argv) {
}