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: void Sub(vector<vector<vector<int> > >&total,vector<int>& a,int i){ if(total[i].size()!=0){ return; } else{ if(i==0){ vector<int> one; one.push_back(a[0]); total[0].push_back(one); total[0].push_back(vector<int>()); return; } else{ Sub(total,a,i-1); vector<vector<int> >r1=total[i-1]; for(int j=0;j<r1.size();j++){ total[i].push_back(r1[j]); r1[j].push_back(a[i]); total[i].push_back(r1[j]); } return; } } } vector<vector<int> > subsets(vector<int> &S) { // Start typing your C/C++ solution below // DO NOT write int main() function if(S.size()==0)return vector<vector<int> >(); sort(S.begin(),S.end()); vector<vector<vector<int> > > total(S.size()); Sub(total,S,S.size()-1); vector<vector<int> >& ret=total[S.size()-1]; set<vector<int> > res; for(int i=0;i<ret.size();i++)res.insert(ret[i]); ret=vector<vector<int> >(res.begin(),res.end()); return ret; } };