-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78.subsets.cpp
More file actions
36 lines (35 loc) · 829 Bytes
/
78.subsets.cpp
File metadata and controls
36 lines (35 loc) · 829 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
/*
* @lc app=leetcode id=78 lang=cpp
*
* [78] Subsets
*/
#include <vector>
using namespace std;
// @lc code=start
class Solution
{
public:
vector<vector<int>> subsets(vector<int> &nums)
{
vector<vector<int>> res;
vector<int> tmp;
for (int i = 0; i <= nums.size(); i++)
c(0, 0, i, nums, tmp, res);
return res;
}
void c(int idx, int cur_level, int target_level, vector<int> &nums, vector<int> &tmp, vector<vector<int>> &res)
{
if (target_level == cur_level)
{
res.push_back(tmp);
return;
}
for (int i = idx; i < nums.size(); i++)
{
tmp.push_back(nums[i]);
c(i + 1, cur_level + 1, target_level, nums, tmp, res);
tmp.pop_back();
}
}
};
// @lc code=end