-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path47-permutations-ii.cpp
42 lines (40 loc) · 997 Bytes
/
47-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
33
34
35
36
37
38
39
40
41
42
class Solution {
public:
int s;
int index;
vector<vector<int>> res;
vector<pair<int, int>> n;
unordered_map<int, int> m;
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
s = nums.size();
index = 0;
for (auto x : nums) {
if (m.find(x) != m.end()) {
n[m[x]].second++;
} else {
m[x] = index;
index++;
n.push_back(make_pair(x, 1));
}
}
vector<int> r;
cal(r);
return res;
}
void cal(vector<int> r) {
if (r.size() == s) {
res.push_back(r);
} else {
for (int i = 0; i < index; i++) {
if (n[i].second != 0) {
n[i].second--;
r.push_back(n[i].first);
cal(r);
r.pop_back();
n[i].second++;
}
}
}
}
};