-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutations II.txt
29 lines (29 loc) · 992 Bytes
/
Permutations II.txt
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
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > rets;
vector<int> ret;
sort(num.begin(), num.end());
vector<bool> use(num.size(), false);
solve(rets, ret, num, use, 0);
return rets;
}
void solve(vector<vector<int>> &rs, vector<int> &r, vector<int> &n, vector<bool> &u, int p) {
if (p == n.size())
rs.push_back(r);
else if (p < n.size()){
int last = -1;
for (int i = 0; i < n.size(); i++)
if (!u[i] && ((last == -1) || (n[i] != n[last]))) {
r.push_back(n[i]);
u[i] = true;
solve(rs, r, n, u, p + 1);
u[i] = false;
r.pop_back();
last = i;
}
}
}
};