码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】Subsets

时间:2015-05-13 07:37:42      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

【leetcode】Subsets

标签:

原文地址:http://www.cnblogs.com/jawiezhu/p/4499257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!