forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0041.cpp
32 lines (31 loc) · 784 Bytes
/
0041.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int firstMissingPositive(vector<int>& nums)
{
for (unsigned int i = 0; i < nums.size(); ++i)
{
while (nums[i] > 0 and nums[i] <= nums.size()
and nums[nums[i] - 1] != nums[i])
{
swap(nums[nums[i] - 1], nums[i]);
}
}
for (unsigned int i = 0; i < nums.size(); ++i)
{
if (nums[i] != i + 1) return i + 1;
}
return nums.size() + 1;
}
};
int main()
{
vector<int> nums = {3,4,-1,1};
cout << Solution().firstMissingPositive(nums);
return 0;
}