-
Notifications
You must be signed in to change notification settings - Fork 0
/
41_Find-Missing-Positive.cpp
56 lines (48 loc) · 1.52 KB
/
41_Find-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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public:
int set_sol(vector<int>&nums){
unordered_set<int> s(nums.begin(), nums.end());
for(int i=1;i<INT_MAX;i++){
if(!s.count(i)) return i;
}
return -1;
}
int swap_sol(vector<int>&nums){
//Place nums[i] into its corresponding index, which is nums[i]-1.
int n=nums.size();
for(int i=0;i<nums.size();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<nums.size();i++){
if(nums[i]==i+1) continue;
return i+1;
}
return n+1;
}
int firstMissingPositive(vector<int>& nums) {
//#1 Set
//return set_sol(nums);
//#1 Swap
return swap_sol(nums);
}
};
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
// Step 1: Place each number in its correct position, such that nums[i] = i + 1
for(int i = 0; i < n; ++i){
while(nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] != nums[i]){
swap(nums[i], nums[nums[i] - 1]);
}
}
// Step 2: Find the first position where the number is not i + 1
for(int i = 0; i < n; ++i){
if(nums[i] != i + 1) {
return i + 1; // Return the missing positive integer
}
}
// If all positions are correctly aligned, the missing number is n + 1
return n + 1;
}
};