-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·91 lines (79 loc) · 2.1 KB
/
main.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
85
86
87
88
89
90
91
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
// BFS based method, rst is used as queue, totally 2^n subsets
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> rst;
if (nums.empty()) return rst;
rst.push_back(vector<int>());
// only loop totally size() position;
for (int i = 0; i < nums.size(); ++i) {
int size = rst.size();
// only loop totally size rounds;
// in essence, this will double the size of rst;
// thus, finally the size of rst is 2^n
for (int j = 0; j < size; ++j) {
rst.push_back(rst[j]);
rst.back().push_back(nums[i]);
}
}
return rst;
}
vector<vector<int>> result;
void helper(vector<int>& nums, int start, vector<int>& tmpbag) {
for (int cnt = start; cnt < nums.size(); ++cnt) {
tmpbag.push_back(nums[cnt]);
helper(nums, cnt + 1, tmpbag);
result.push_back(tmpbag);
tmpbag.pop_back();
}
}
// recursively method, treat it as DFS
vector<vector<int>> subsets2(vector<int>& nums) {
vector<int> tmpbag;
result.push_back(tmpbag);
helper(nums, 0, tmpbag);
return result;
}
void getSubSets(vector<int>& nums, int pos, vector<int> &out, vector<vector<int>> &rst) {
// deal with the end case
if (pos == nums.size()) {
rst.push_back(out);
return;
}
for (int i = pos; i < nums.size(); ++i) {
out.push_back(nums[i]);
getSubSets(nums, pos+1, out, rst);
out.pop_back();// do not choose the current val
}
}
// recursively method, treat it as DFS
vector<vector<int>> subsets3(vector<int>& nums) {
vector<vector<int>> rst;
vector<int> out;
rst.push_back(vector<int>());
getSubSets(nums, 0, out, rst);
return rst;
}
};
ostream & operator<< (ostream &os, vector<vector<int>> &rtn) {
os << "[";
for (auto & val : rtn) {
os << "[";
for (auto i : val) {
os << i << ", ";
}
os << "], ";
}
os << "]";
return os;
}
int main() {
Solution gua;
vector<int> nums{ 1,2,3 };
vector<vector<int>> rst = gua.subsets(nums);
cout << rst << endl;
return 0;
}