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.
For example,
If S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
集合中的一个元素选择或者不选择。
class Solution {
public:
vector<vector<int> > ans;
vector<int> row;
vector<vector<int> > subsets(vector<int> &S) {
if(S.size()==0) return ans;
sort(S.begin(),S.end());
generate(row,S,0);
return ans;
}
void generate(vector<int> row,vector<int> &S,int i){
if(i == S.size()){
ans.push_back(row);
}
else{
generate(row,S,i+1);
row.push_back(S[i]);
generate(row,S,i+1);
}
}
};