-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsubsets.cpp
More file actions
35 lines (30 loc) · 926 Bytes
/
subsets.cpp
File metadata and controls
35 lines (30 loc) · 926 Bytes
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
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
return powerset(S, S.size()-1);
}
vector<vector<int> > powerset(vector<int> &S, int end) {
vector<vector<int> > sets;
if ( S.size() == 0 ) {
vector<int> set;
sets.push_back(set);
return sets;
}
if ( end == 0 ) {
vector<int> set;
sets.push_back({});
set.push_back(S[end]);
sets.push_back(set);
return sets;
}
vector<vector<int> > prev_sets = powerset(S, end - 1);
for ( auto it = prev_sets.begin(); it != prev_sets.end(); it++ ) {
vector<int> set = *it;
sets.push_back(*it);
set.push_back(S[end]);
sets.push_back(set);
}
return sets;
}
};