-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubsets.cpp
53 lines (46 loc) · 981 Bytes
/
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
47
48
49
50
51
52
53
#include<vector>
#include<iostream>
using namespace std;
vector<vector<int>> ans;
void sub(vector<int>& nums, int i, vector<int> temp)
{
if (i == nums.size())
{
ans.push_back(temp);
return;
}
sub(nums, i + 1, temp);
temp.push_back(nums[i]);
sub(nums, i + 1, temp);
}
vector<vector<int>> subsets(vector<int>& nums) {
vector<int> temp;
sub(nums, 0, temp); // or sub(nums, 0, vector<int> {});
return ans;
}
void dfs(int pos, vector<int>& temp, vector<vector<int>>& result, vector<int>& nums)
{
if (pos == nums.size())
{
result.push_back(temp);
return;
}
dfs(pos + 1, temp, result, nums);
temp.push_back(nums[pos]);
dfs(pos + 1, temp, result, nums);
}
//vector<vector<int>> subsets(vector<int>& nums)
//{
//
// vector<vector<int>> result;
// vector<int> temp;
// result.push_back(temp);
// dfs(0, temp, result, nums);
// return result;
//}
int mainSubsets()
{
vector<int> nums{ 1,2,3 };
subsets(nums);
return 0;
}