-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutations II
42 lines (36 loc) · 971 Bytes
/
Permutations II
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
35
36
37
38
39
40
41
42
class Solution {
public:
vector<vector<int> > res;
void search(vector<int>& num, vector<int>& cur)
{
if (num.empty())
{
res.push_back(cur);
return;
}
int prev = 0, len = num.size();
for (int i=0; i<len; ++i)
{
if (i>0 && num[i]==prev)
continue;
else
{
prev = num[i];
cur.push_back(num[i]);
num.erase(num.begin()+i);
search(num, cur);
num.insert(num.begin()+i, prev);
cur.pop_back();
}
}
}
vector<vector<int> > permuteUnique(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
sort(num.begin(), num.end());
vector<int> tmp;
search(num, tmp);
return res;
}
};