-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets_II.cpp
More file actions
35 lines (30 loc) · 869 Bytes
/
Subsets_II.cpp
File metadata and controls
35 lines (30 loc) · 869 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
#include <vector>
#include <algorithm>
class Solution {
private:
std::vector<std::vector<int>> answer;
public:
std::vector<std::vector<int>> subsetsWithDup(std::vector<int>& nums) {
answer.push_back({});
sort(nums.begin(), nums.end());
helper(nums, {}, 0);
return answer;
}
void helper(std::vector<int>& nums, std::vector<int> curr, int index) {
if (index == nums.size()) {
// max length subset built
return;
}
for (int i=index; i<nums.size(); i++) {
curr.push_back(nums[i]);
answer.push_back(curr);
helper(nums, curr, i+1);
curr.pop_back();
// Skip to the next unique element
while (i<nums.size()-1 && nums[i] == nums[i+1]) {
i++;
}
}
return;
}
};