-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations II.h
More file actions
33 lines (30 loc) · 1012 Bytes
/
Permutations II.h
File metadata and controls
33 lines (30 loc) · 1012 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
33
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
sort(num.begin(), num.end());
vector<vector<int> > res;
generatePermutations(num, 0, res);
return res;
}
private:
void generatePermutations(vector<int> &num, int pos, vector<vector<int> > &res) {
if(pos == num.size()-1) {
vector<int> copy(num);
res.push_back(copy);
return;
}
int prev = num[pos] -1;//not equal to num[pos]
for(int i = pos; i < num.size(); ++i) {
if(num[i] == prev) continue;
prev = num[i];
move(i, pos, num); // cannot use swap here, bacause swap will break the order of numbers
generatePermutations(num, pos+1, res);
move(pos, i, num);
}
}
void move(int from, int to, vector<int> &num) {
int tmp = num[from];
num.erase(num.begin()+from);
num.insert(num.begin()+to, tmp);
}
};