Yongzhang’s Blog Subsets

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],
[]
]

vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(),S.end());
vector<vector<int> >res = getSubsets(S,0);
return res;
}
vector<vector<int> > getSubsets(vector<int>& s, int idx){
vector<vector<int> >res;
if(idx == s.size()){
vector<int>t;
res.push_back(t);
return res;
}
vector<vector<int> >temp = getSubsets(s,idx+1);
for(int i=0; i < temp.size(); i++){
res.push_back(temp[i]);
temp[i].insert(temp[i].begin(),s[idx]);
res.push_back(temp[i]);
}
return res;
}