-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path90.cpp
More file actions
48 lines (40 loc) · 959 Bytes
/
Copy path90.cpp
File metadata and controls
48 lines (40 loc) · 959 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "common.h"
using namespace std;
class Solution {
public:
vector<vector<int>> rst;
vector<vector<int>> subsetsWithDup(vector<int> &nums) {
std::sort(nums.begin(), nums.end());
vector<int> v;
dfs(nums, v, 0);
rst.emplace_back();
return rst;
}
void dfs(vector<int>& nums, vector<int> subset, int index) {
if (index >= nums.size()) {
return;
}
int new_index = index + 1;
while (new_index < nums.size() && nums[index] == nums[new_index]) {
new_index += 1;
}
dfs(nums, subset, new_index);
for (int i = index; i < new_index; ++i) {
subset.push_back(nums[index]);
rst.push_back(subset);
dfs(nums, subset, new_index);
}
}
};
int main() {
Solution s;
vector<int> v = {1, 2, 2};
auto rst = s.subsetsWithDup(v);
for (const auto &kItem : rst) {
for (const auto &kItem2 : kItem) {
cout << kItem2 << " ";
}
cout << endl;
}
return 0;
}