-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet41.cpp
52 lines (47 loc) · 989 Bytes
/
leet41.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
50
51
52
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <functional>
#include <cmath>
#include <set>
#include <map>
#include <assert.h>
#include <stack>
#include <unordered_map>
using namespace std;
/*
原地哈希:哈希函数可以看作是把nums[i]交换到nums[i]-1位置上
*/
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int N = nums.size();
for(int i=0;i<N;i++)
{
while(nums[i]>0 && nums[i]<=N && nums[i]!=nums[nums[i]-1])
{
swap(nums[i],nums[nums[i]-1]);
}
}
for(int i=0;i<N;i++)
{
if(nums[i]!=i+1)
return i+1;
}
return N+1;
}
};
int main()
{
vector<int> t = {3,4,-1,1,9,-5};
Solution sol;
int x = sol.firstMissingPositive(t);
return 0;
}