forked from dreamerHarshit/Coding_Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_subsets.cpp
More file actions
41 lines (39 loc) · 951 Bytes
/
all_subsets.cpp
File metadata and controls
41 lines (39 loc) · 951 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
38
39
40
41
/*
Subset
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.
Also, the subsets should be sorted in ascending ( lexicographic ) order.
The list is not necessarily sorted.
Example : If S = [1,2,3], a solution is:
[
[],
[1],
[1, 2],
[1, 2, 3],
[1, 3],
[2],
[2, 3],
[3],
]
*/
void find_subset(vector<int> &A, vector<vector<int>> &result, vector<int> &aux, int i){
if(i==A.size()){
result.push_back(aux);
return;
}
find_subset(A,result,aux,i+1);
aux.push_back(A[i]);
find_subset(A,result,aux,i+1);
aux.pop_back();
}
vector<vector<int> > Solution::subsets(vector<int> &A) {
vector<int> aux;
vector<vector<int> > result;
int i = 0;
sort(A.begin(),A.end());
find_subset(A, result, aux, i);
sort(result.begin(),result.end());
return result;
}