-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#64.cc
36 lines (31 loc) · 910 Bytes
/
LeetCode#64.cc
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
class Solution {
private:
void dfs(int ind,int cnt,int size,int k,vector<int>& S,
vector<int>& tmp,vector<vector<int> >& res){
if(cnt==k){
res.push_back(tmp);
return ;
}
for(int i=ind;i<size;i++){
if(i==ind || S[i]!=S[i-1]){
tmp[cnt]=S[i];
dfs(i+1,cnt+1,size,k,S,tmp,res);
}
}
}
public:
vector<vector<int> > subsets(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > ret;
ret.push_back(vector<int>());
int size = S.size();
if(size==0) return ret;
sort(S.begin(),S.end());
for(int i=1;i<=size;i++){
vector<int> tmp(i,0);
dfs(0,0,size,i,S,tmp,ret);
}
return ret;
}
};