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 {
public:
void dfs(vector<int> &nums,vector<vector<int>> &result,int step,vector<int> &tmp)
{
result.push_back(tmp);//不需要所有的数字都参与
//如果需要所有的数字都参与
/*if(step == nums.size())
{
result.push_back(tmp);
return;
}*/
for(int i=step;i<nums.size();i++)
{
tmp.push_back(nums[i]);
dfs(nums,result,i+1,tmp); //到底什么情况是dfs(nums,result,i+1,tmp),什么情况是dfs(nums,result,step+1,tmp),需要仔细分析
tmp.pop_back();
}
}
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> result;
if(nums.size()<=0)
return result;
vector<int> tmp;
sort(nums.begin(),nums.end());
dfs(nums,result,0,tmp);
return result;
}
};