###### 78. Subsets

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

class Solution {
private:
vector<vector<int>> res;
public:
vector<vector<int>> subsets(vector<int>& nums) {
res.clear();
sort(nums.begin(),nums.end());
vector<int> tmp;
dfs(nums,0,tmp);
return res;
}
private:
void dfs(vector<int> nums,int index,vector<int>& tmp){
if(index == nums.size()){
res.push_back(tmp);
return;
}
tmp.push_back(nums[index]);
dfs(nums,index+1,tmp);
tmp.pop_back();
dfs(nums,index+1,tmp);
}
};

1处理完成之后，我们可以同样方式处理2，以及3。

class Solution {
public:
vector<vector<int> > res;
vector<vector<int> > subsets(vector<int> &S) {
res.clear();
vector<int> tmp;
//sort(S.begin(),S.end());
res.push_back(vector<int>());
dfs(S,0,tmp);
return res;
}
void dfs(vector<int> nums, int start, vector<int>& tmp){
if(start == nums.size()){
return ;
}
for(int i = start; i<nums.size(); i++){
tmp.push_back(nums[i]);
res.push_back(tmp);
dfs(nums,i+1,tmp);
tmp.pop_back();
}
}
};

subsets Ⅱ 是在subsetⅠ的基础上去重。思想类似。

class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
res.clear();
sort(nums.begin(),nums.end());
res.push_back(vector<int>());
vector<int> tmp;
dfs(nums,0,tmp);
return res;
}
void dfs(vector<int> nums, int start, vector<int> &tmp){
if(start == nums.size()){
return ;
}
vector<vector<int>>::iterator it = res.begin();
for(int i = start; i<nums.size(); i++){
tmp.push_back(nums[i]);
res.push_back(tmp);
dfs(nums,i+1,tmp);
tmp.pop_back();
while(i<nums.size()-1 && nums[i]==nums[i+1]){
i++;
}
}
}
};

#### leetcode 78. Subsets-数组子集|回溯算法

2016-06-07 17:04:45

#### LeetCode 78. Subsets 解题报告

2016-02-17 09:09:55

#### LeetCode OJ 系列之78 Subsets --Python

2015-11-18 22:07:50

#### leetcode解题之 77. Combinations&78. Subsets&90. Subsets II java 版（求所有子集）

2017-04-08 19:36:52

#### [leetcode]78. Subsets -- JavaScript 代码

2016-08-23 11:17:35

#### [C++]LeetCode: 63 Subsets

2014-12-30 21:13:48

#### [leetcode-78]subsets(java)

2015-08-05 22:42:55

#### 两种回溯方法解决子集问题的思路 leetcode 78. Subsets

2017-02-16 10:34:34

#### 78. Subsets(Python3)

2018-01-29 13:44:57

#### leetCode 78.Subsets (子集) 解题思路和方法

2015-07-22 13:18:53

78. Subsets