-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path78.cpp
84 lines (83 loc) · 2.63 KB
/
78.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
__________________________________________________________________________________________________
8ms
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>>res(1);
for(int i = 0; i<nums.size(); ++i){
int size = res.size();
for(int j = 0; j<size; ++j){
res.push_back(res[j]);
res.back().push_back(nums[i]);
}
}
return res;
}
};
__________________________________________________________________________________________________
8812 kb
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums)
{
// 【二周目】
vector<vector<int>> results;
// sort [nums] into non-descending order
sort(nums.begin(), nums.end());
vector<int> subset;
// [1]-root has nums.size() children and every node in tree is a subset
// DFS_helper_1(results, subset, nums, 0);
// [2]-it is a binary tree and every leaf is a subset
// DFS_helper_2(results, subset, nums, 0);
// [3]-use bit operation
BitHelper(results, subset, nums);
return results;
}
private:
void DFS_helper_1(vector<vector<int>>& results,
vector<int>& subset,
vector<int>& nums,
int start)
{
results.push_back(subset);
for (int i = start; i < nums.size(); i ++)
{
subset.push_back(nums[i]);
DFS_helper_1(results, subset, nums, i + 1);
subset.pop_back();
}
}
void DFS_helper_2(vector<vector<int>>& results,
vector<int>& subset,
vector<int>& nums,
int index)
{
if (index == nums.size())
{
results.push_back(subset);
return;
}
subset.push_back(nums[index]);
DFS_helper_2(results, subset, nums, index + 1);
subset.pop_back();
DFS_helper_2(results, subset, nums, index + 1);
}
void BitHelper(vector<vector<int>>& results,
vector<int>& subset,
vector<int>& nums)
{
int amount = 1 << nums.size();
for (int i = 0; i < amount; i ++)
{
subset.clear();
for (int j = 0; j < nums.size(); j ++)
{
int tmp = 1 << j;
if (i & tmp)
subset.push_back(nums[j]);
}
results.push_back(subset);
}
}
};
__________________________________________________________________________________________________