Skip to content

Commit d9bfc42

Browse files
committed
3
1 parent 5b44e68 commit d9bfc42

File tree

1 file changed

+6
-49
lines changed

1 file changed

+6
-49
lines changed
+6-49
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,19 @@
11
class Solution {
22
public:
33
int firstMissingPositive(int A[], int n) {
4-
// Start typing your C/C++ solution below
5-
// DO NOT write int main() function
6-
74
int i = 0;
85
while (i < n) {
9-
if (A[i] != i + 1 && 0 < A[i] && A[i] <= n && A[A[i]-1] != A[i])
6+
if (0 < A[i] && A[i] <= n && A[i] != A[A[i]-1]) {
107
swap(A[i], A[A[i]-1]);
11-
else
12-
i += 1;
8+
} else {
9+
i++;
10+
}
1311
}
14-
int first_positive = 0;
1512
for (int i = 0; i < n; i++) {
1613
if (A[i] != i + 1) {
17-
first_positive = i + 1;
18-
break;
14+
return i + 1;
1915
}
2016
}
21-
if (first_positive == 0)
22-
first_positive = n + 1;
23-
return first_positive;
17+
return n + 1;
2418
}
2519
};
26-
27-
//
28-
// solution 2
29-
//
30-
class Solution {
31-
public:
32-
int firstMissingPositive(int A[], int n) {
33-
// Start typing your C/C++ solution below
34-
// DO NOT write int main() function
35-
36-
bool find_1 = false;
37-
for (int i = 0; i < n; i++) {
38-
if (A[i] == 1)
39-
find_1 = true;
40-
else if (A[i] <= 0)
41-
A[i] = 1;
42-
}
43-
if (!find_1) return 1;
44-
for (int i = 0; i < n; i++) {
45-
if (A[i] > 0 && A[i] <= n && A[A[i]-1] > 0) {
46-
A[A[i]-1] = -A[A[i]-1];
47-
}
48-
else if (A[i] <= -1 && -A[i] <= n && A[-A[i]-1] > 0)
49-
A[-A[i]-1] = -A[-A[i]-1];
50-
}
51-
int first_positive = 0;
52-
for (int i = 0; i < n; i++) {
53-
if (A[i] > 0) {
54-
first_positive = i + 1;
55-
break;
56-
}
57-
}
58-
if (first_positive == 0)
59-
first_positive = n + 1;
60-
return first_positive;
61-
}
62-
};

0 commit comments

Comments
 (0)