-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets.cpp
More file actions
37 lines (32 loc) · 980 Bytes
/
Subsets.cpp
File metadata and controls
37 lines (32 loc) · 980 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
36
37
class Solution {
public:
void findNextElement(vector<int> S, vector<vector<int> > &result, vector<int>oneS,
int index, int length)
{
while(index<length)
{
oneS.push_back(S[index]);
result.push_back(oneS);
findNextElement(S, result, oneS, index+1, length);
oneS.pop_back();
index++;
}
}
vector<vector<int> > subsets(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int length = S.size();
vector<vector<int> > result;
sort(S.begin(), S.end());
vector<int> oneS;
result.push_back(oneS);
for(int i=0; i<length; i++)
{
oneS.push_back(S[i]);
result.push_back(oneS);
findNextElement(S, result, oneS, i+1, length);
oneS.pop_back();
}
return result;
}
};