Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]
ac代码:
class Solution { public: vector<vector<int> > subsets(vector<int> &S) { vector<vector<int> >result; vector<int>path; int i,L=S.size(); result.push_back(path); for(i=1;i<=L;i++) { DFS(S,L,i,0,path,result); } return result; } void DFS(vector<int> &S,int n,int k,int index,vector<int>&path,vector<vector<int> >&result) { if(path.size()==k) { result.push_back(path); return ; } int i; for(i=index;i<n;i++) { path.push_back(S[i]); DFS(S,n,k,i+1,path,result); path.pop_back(); } } };