-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0078_subsets.cc
52 lines (47 loc) · 1021 Bytes
/
Problem_0078_subsets.cc
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
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 二进制枚举
vector<vector<int>> subsets1(vector<int> &nums)
{
int mask = 1 << nums.size();
vector<vector<int>> ans;
for (int i = 0; i < mask; i++)
{
vector<int> cur;
for (int j = 0; j < nums.size(); j++)
if ((i >> j) & 1)
{
cur.push_back(nums[j]);
}
ans.emplace_back(cur);
}
return ans;
}
void process(vector<int> &nums, int index, vector<int> &cur, vector<vector<int>> &ans)
{
if (index == nums.size())
{
ans.push_back(cur);
return;
}
// 不取index位置
process(nums, index + 1, cur, ans);
// 取index位置
cur.push_back(nums[index]);
process(nums, index + 1, cur, ans);
cur.pop_back();
}
// 递归
vector<vector<int>> subsets2(vector<int> &nums)
{
vector<vector<int>> ans;
vector<int> cur;
process(nums, 0, cur, ans);
return ans;
}
};