-
Notifications
You must be signed in to change notification settings - Fork 0
/
First_Missing_Positive
68 lines (54 loc) · 1.09 KB
/
First_Missing_Positive
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
66
67
68
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
//find the smallest and the largest numbers
int s, l;
s = l = 0;
int c = 0;
vector<int> dict;
for (auto it = nums.begin(); it != nums.end(); ++it)
{
if (*it > 0)
{
s = l = *it;
break;
}
}
if ((nums.size() == 0) || (nums.size() == 1 && nums.at(0) == 0))
return 1;
if (nums.size() == 1)
if(nums.at(0) > 1)
return 1;
else
return nums.at(0) + 1;
for (auto it = nums.begin(); it != nums.end(); ++it) {
if (*it<0 || *it == 0)
continue;
if (*it <= s && *it>0)
s = *it;
else if (*it >= l)
l = *it;
if(std::find(dict.begin(),dict.end(),*it)==dict.end())
dict.push_back(*it);
}
if (s > 1)
return 1;
if (s == l && s - 1 > 0)
return s - 1;
else if (s == l && s - 1 <= 0)
return s + 1;
int count = 0;
for (int i = s; i <= l; i++) {
if(std::count(dict.begin(), dict.end(), i)==0)
return i;
++count;
}
if (count == dict.size()) {
if ((s - 1)>0)
return 1;
else
return (l + 1);
}
return s;
}
};