-
Notifications
You must be signed in to change notification settings - Fork 5
/
day-102.cpp
51 lines (42 loc) · 1.02 KB
/
day-102.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
43
44
45
46
47
48
49
50
51
/*
Subsets
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
*/
// Recursive solution to make subsets
class Solution {
public:
void subsetsHelper(vector<int>& nums, int idx, vector<vector<int>>& answer) {
if (idx == nums.size() - 1) {
answer.push_back({nums[idx]});
answer.push_back({});
} else {
subsetsHelper(nums, idx + 1, answer);
int N = answer.size();
for (int i = 0; i < N; i++) {
vector<int> curr = answer[i];
curr.push_back(nums[idx]);
answer.push_back(curr);
}
}
}
vector<vector<int>> subsets(vector<int>& nums) {
if (!nums.size()) return {{}};
vector<vector<int>> answer;
subsetsHelper(nums, 0, answer);
return answer;
}
};