-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst Missing Positive.cpp
38 lines (35 loc) · 1.33 KB
/
First Missing Positive.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
/*
Given an unsorted integer array, find the first missing positive integer.
Link: http://www.lintcode.com/en/problem/first-missing-positive/
Example:
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Solution: This is not similar to find first missing number because the numbers in this list are not all positive.
Moreover, it also has the problem on memory usage. Then, we perform bucket sort, and find the first value which
not fulfil the equation that value = index + 1. In this process, we use a definition that the sorted value should
located at index that value minus one. Then, if we meet a value which are not in its suitable location, we change
it to its suitable location (assume the list have enough space).
Source: None
*/
class Solution {
public:
/**
* @param A: a vector of integers
* @return: an integer
*/
int firstMissingPositive(vector<int> A) {
// write your code here
for (int i = 0; i < A.size(); i++) {
while (A[i] > 0 && A[i] <= A.size() &&
A[i] != i + 1 && A[A[i] - 1] != A[i]) {
swap(A[A[i] - 1], A[i]);
}
}
for (int i = 0; i < A.size(); i++) {
if (A[i] != i + 1) {
return i + 1;
}
}
return A.size() + 1;
}
};