-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubset.cpp
More file actions
50 lines (46 loc) · 949 Bytes
/
Subset.cpp
File metadata and controls
50 lines (46 loc) · 949 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
42
43
44
45
46
47
48
49
50
/*Question:
Subset
Asked in:
Amazon
Microsoft
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],
]
Seen this question in a real interview before*/
vector<vector<int>> z;
void foo(vector<int> A,vector<int> ret,int i)
{
if(i>=A.size())
{
// cout<<"i"<<endl;
z.push_back(ret);
return ;
}
foo(A,ret,i+1);
ret.push_back(A[i]);
foo(A,ret,i+1);
ret.pop_back();
}
vector<vector<int> > Solution::subsets(vector<int> &A) {
sort(A.begin(),A.end());
vector<int> ret;
z.clear();
foo(A,ret,0);
sort(z.begin(),z.end());
return z;
}