标签:
Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
1 class Solution { 2 private: 3 vector<vector<int> >ret; 4 5 public: 6 void dfs(int dep,int maxDep,vector<int> &num,vector<int> a,int start) 7 { 8 ret.push_back(a); 9 10 if(dep==maxDep) 11 return ; 12 13 for(int i=start;i<num.size();i++) 14 { 15 vector<int> b(a); 16 b.push_back(num[i]); 17 dfs(dep+1,maxDep,num,b,i+1); 18 } 19 } 20 21 vector<vector<int> > subsets(vector<int>& nums) { 22 sort(nums.begin(),nums.end()); 23 ret.clear(); 24 vector<int> a; 25 dfs(0,nums.size(),nums,a,0); 26 27 return ret; 28 } 29 };
标签:
原文地址:http://www.cnblogs.com/jawiezhu/p/4499257.html