-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_78_Subsets.cpp
46 lines (38 loc) · 1.03 KB
/
LC_78_Subsets.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
/*
https://leetcode.com/problems/subsets/
78. Subsets
*/
class Solution {
public:
vector<vector<int>> ans;
int n;
vector<vector<int>> subsets(vector<int>& nums) {
n = nums.size();
vector<int> subset;
backtrackHelper(nums, subset, 0);
return ans;
}//end
void backtrackHelper11(vector<int>& input, vector<int> output, int idx)
{
// Recursive
if(idx == input.size())
{
ans.push_back(output);
return ;
}
output.push_back(input[idx]);
backtrackHelper11(input, output, idx+1);
output.pop_back();
backtrackHelper11(input, output, idx+1);
}
void backtrackHelper(vector<int>& input, vector<int>& subset, int idx)
{
ans.push_back(subset);
for(int i=idx; i<input.size(); i++)
{
subset.push_back(input[i]);
backtrackHelper(input, subset, i+1);
subset.pop_back();
}
}
};