-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0078.subsets.cpp
46 lines (36 loc) · 912 Bytes
/
0078.subsets.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
#include <iostream>
#include <vector>
#include "leetcode.h"
using std::vector;
vector<vector<int>> subset(vector<int>& nums)
{
int n = nums.size();
vector<vector<int>> res;
vector<int> path;
if (n == 0) return res;
auto backtracking = [&nums, &res, n](auto&& self, vector<int>& path, int t) -> void {
res.push_back(path);
for (int i = t; i < n; ++i) {
path.push_back(nums[i]);
self(self, path, i + 1);
path.pop_back();
}
};
backtracking(backtracking, path, 0);
return res;
}
int main () {
#ifdef LOCAL
freopen("0078.in", "r", stdin);
#endif
int n = 0;
while (std::cin >> n) {
vector<int> nums(n, 0);
for (int i = 0; i < n; ++i) {
std::cin >> nums[i];
}
vector<vector<int>> res = subset(nums);
std::cout << res << std::endl;
}
return 0;
}