-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsetII.cpp
More file actions
46 lines (43 loc) · 896 Bytes
/
SubsetII.cpp
File metadata and controls
46 lines (43 loc) · 896 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
/*QUestion:
Subsets II
Asked in:
Amazon
Microsoft
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
The subsets must be sorted lexicographically.
Example :
If S = [1,2,2], the solution is:
[
[],
[1],
[1,2],
[1,2,2],
[2],
[2, 2]
] */
vector<vector<int>> z;
void foo(vector<int> A,vector<int> ret,int i)
{
if(i>=A.size())
{
// cout<<"i"<<endl;
if(find(z.begin(),z.end(),ret)==z.end())
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::subsetsWithDup(vector<int> &A) {
sort(A.begin(),A.end());
vector<int> ret;
z.clear();
foo(A,ret,0);
sort(z.begin(),z.end());
return z;
}