-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
67 lines (54 loc) · 1.29 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
#include <iostream>
#include <vector>
using std::vector;
class Solution {
vector<vector<int>> result;
vector<int> path;
void backtracking(const vector<int> &nums, int index) {
if (index == nums.size()) {
result.push_back(path);
return;
}
path.push_back(nums[index]);
backtracking(nums, index + 1);
path.pop_back();
backtracking(nums, index + 1);
}
public:
vector<vector<int>> subsets(vector<int> &nums) {
result.clear();
path.clear();
backtracking(nums, 0);
return result;
}
};
template <typename T> void output(const vector<vector<T>> &sets, int index) {
std::cout << "case" << index << ":";
std::cout << "[";
for (int i = 0; i < sets.size(); i++) {
std::cout << "[";
for (int j = 0; j < sets[i].size(); j++) {
std::cout << sets[i][j];
if (j != (sets[i].size() - 1)) {
std::cout << ",";
}
}
std::cout << "]";
if (i != (sets.size() - 1)) {
std::cout << ",";
}
}
std::cout << "]" << std::endl;
}
// Example 1:
// Input: nums = [1,2,3]
// Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
// Example 2:
// Input: nums = [0]
// Output: [[],[0]]
int main(int argc, char *argv[]) {
vector<int> nums{1, 2, 3};
Solution solution;
output(solution.subsets(nums), 1);
return 0;
}