-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path90.cpp
More file actions
31 lines (30 loc) · 957 Bytes
/
Copy path90.cpp
File metadata and controls
31 lines (30 loc) · 957 Bytes
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
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums) {
vector<vector<int>> ret;
vector<int> temp;
int n = nums.size();
sort(nums.begin(),nums.end());
BackTracking(nums, ret, temp, 0, n);
map<vector<int>, bool> exist;
for (int i = 0; i < ret.size(); ++i) {
if (exist.find(ret[i]) == exist.end()) {
exist.insert(map<vector<int>, bool>::value_type(ret[i], true));
} else {
ret.erase(ret.begin() + i);
--i;
}
}
return ret;
}
void BackTracking(vector<int> &num, vector<vector<int>> &ret, vector<int> &temp, int i, int &n) {
ret.push_back(temp);
if (i == n)
return;
for (int j = i; j < n; ++j) {
temp.push_back(num[j]);
BackTracking(num, ret, temp, j + 1, n);
temp.pop_back();
}
}
};