-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathsolution.cpp
34 lines (31 loc) · 836 Bytes
/
solution.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
33
34
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num)
{
int len = num.size();
vector<vector<int> > res;
sort(num.begin(), num.end());
gen(res, num, 0, len);
return res;
}
bool isSwap(vector<int>& num, int s, int e)
{
int i = s;
while (num[i] != num[e] && i < e) i++;
if (i == e) return true;
else return false;
}
void gen(vector<vector<int> > &res, vector<int>& num, int cur, int len)
{
if (cur == len) {
res.push_back(num);
return;
}
for (int i = cur; i < len; i++) {
if (!isSwap(num, cur, i)) continue;
swap(num[cur], num[i]);
gen(res, num, cur+1, len);
swap(num[cur], num[i]);
}
}
};