forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationsII.cpp
More file actions
33 lines (32 loc) · 1007 Bytes
/
PermutationsII.cpp
File metadata and controls
33 lines (32 loc) · 1007 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) {
if(num.size()==0) {
return vector<vector<int> >();
}
vector<vector<int> > vs = permute(num, (int)num.size()-1);
set<vector<int> > se(vs.begin(), vs.end());
return vector<vector<int>>(se.begin(), se.end());
}
vector<vector<int> > permute(vector<int> &num, int n) {
vector<vector<int> > res;
if(n==0) {
res.push_back(vector<int>{num[0]});
return res;
}
vector<vector<int> > ps = permute(num, n-1);
for(int i=0;i<ps.size();i++) {
for(int j=0;j<=n;j++){
if(j!=n) {
if(ps[i][j]==num[n]) {
continue;
}
}
vector<int> t(ps[i].begin(), ps[i].end());
t.insert(t.begin()+j,num[n]);
res.push_back(t);
}
}
return res;
}
};