forked from sunstick/code-street
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsets.cpp
More file actions
70 lines (57 loc) · 1.32 KB
/
subsets.cpp
File metadata and controls
70 lines (57 loc) · 1.32 KB
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
/*
Given a set of distinct integers, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
*/
class Solution {
public:
void solve(int x, vector<vector<int> > &res, vector<int> &s) {
vector<int> v;
for (int i = 0; i < s.size(); i++)
if (x & (1 << i))
v.push_back(s[i]);
sort(v.begin(), v.end());
res.push_back(v);
}
vector<vector<int> > subsets(vector<int> &s) {
int n = s.size();
vector<vector<int> > res;
for (int i = 0; i < (1 << n); i++) {
solve(i, res, s);
}
return res;
}
};
class Solution {
public:
void solve(vector<vector<int> > &res, vector<int> &v, int off, vector<int> &s) {
if (off == s.size()) {
res.push_back(v);
return ;
}
solve(res, v, off + 1, s);
v.push_back(s[off]);
solve(res, v, off + 1, s);
v.pop_back();
}
vector<vector<int> > subsets(vector<int> &s) {
sort(s.begin(), s.end());
vector<int> v;
vector<vector<int> > res;
solve(res, v, 0, s);
return res;
}
};