-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations_2.cpp
More file actions
34 lines (28 loc) · 954 Bytes
/
Copy pathPermutations_2.cpp
File metadata and controls
34 lines (28 loc) · 954 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
34
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
if(num.empty()) return vector<vector<int>>();
sort(num.begin(), num.end());
vector<vector<int>> result;
vector<int> sofar;
permute_helper(result, sofar, num);
return result;
}
private:
void permute_helper(vector<vector<int>> &result, vector<int> &sofar, vector<int> &rest){
if(rest.empty()){
result.push_back(sofar);
return;
}
for(int i = 0; i < rest.size(); i++){
// remove the duplication
if(i > 0 && rest[i] == rest[i-1]) continue;
int t = rest[i];
sofar.push_back(t);
auto it = rest.erase(rest.begin()+i);
permute_helper(result, sofar, rest);
sofar.pop_back();
rest.insert(it, t);
}
}
};