-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution.cpp
More file actions
32 lines (30 loc) · 812 Bytes
/
Solution.cpp
File metadata and controls
32 lines (30 loc) · 812 Bytes
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 <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> res ;
for (int i=0; i< nums.size(); ) {
if (nums[i] != i + 1 and nums[nums[i] - 1] != nums[i]) {
//swap
int t = nums[i], p = nums[i] - 1;
nums[i] = nums[nums[i] - 1];
nums[p] = t;
} else i ++ ;
}
for (int i=0; i<nums.size(); i++) {
if (i + 1 != nums[i]) res.push_back(i + 1);
}
return res;
}
};
int main() {
Solution s;
vector<int> v {4,3,2,7,8,2,3,1};
auto x = s.findDisappearedNumbers(v);
for (auto i: x) cout << i << endl;
}