forked from fanfank/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutations-ii.cpp
32 lines (32 loc) · 962 Bytes
/
permutations-ii.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
class Solution { //almost the same as permutations.cpp
public:
int sz;
vector<vector<int> > vv;
void permutation(vector<int> &v, int left, int right) {
if(left == right) {
vv.push_back(v);
return;
}
permutation(v, left + 1, right);
set<int> s;
s.insert(v[left]);
for(int i = left + 1; i <= right; ++i) {
if(s.find(v[i]) == s.end())
s.insert(v[i]);
else
continue;
swap(v[left], v[i]);
permutation(v, left + 1, right);
swap(v[left], v[i]);
}
}
vector<vector<int> > permuteUnique(vector<int> &num) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vv.clear();
sz = num.size();
if(sz > 0)
permutation(num, 0, sz - 1);
return vv;
}
};