-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode041.cpp
49 lines (47 loc) · 1.15 KB
/
leetcode041.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
39
40
41
42
43
44
45
46
47
48
49
/*************************************************************************
> File Name: leetcode041.cpp
> Author:
> Mail:
> Created Time: Sun 04 Sep 2016 11:38:05 PM PDT
************************************************************************/
#include<iostream>
#include<vector>
using namespace std;
int firstMissingPositive(vector<int>& nums) {
int i = 0;
while(i < nums.size())
{
if(nums[i] > 0 && nums[i] != i + 1 && nums[nums[i] - 1] != nums[i] && nums[i] - 1 < nums.size())
{
int temp = nums[nums[i] - 1];
nums[nums[i] - 1] = nums[i];
nums[i] = temp;
}
else
{
i++;
}
}
for(int i = 0 ; i < nums.size();i++)
{
if(nums[i] != i + 1)
{
return i + 1;
}
}
return nums.size() + 1;
}
int main()
{
vector<int> data1;
data1.push_back(1);
data1.push_back(2);
data1.push_back(0);
vector<int> data2;
data2.push_back(3);
data2.push_back(4);
data2.push_back(-1);
data2.push_back(1);
cout << firstMissingPositive(data1) << endl;
cout << firstMissingPositive(data2) << endl;
}