题解 | #集合的子集#
集合的子集
http://www.nowcoder.com/practice/1f2700e2b1904254b55765479e9b8766
class Subset { public: vector<vector>res; vectorpath;
void recur(vector<int>nums,int start_index)
{
if(!path.empty())
{
res.push_back(path);
}
if(nums.size()==start_index)
{
return;
}
//for(int i=start_index;i<nums.size();i++)
for(int i=nums.size()-1;i>=start_index;i--)
{
path.push_back(nums[i]);
recur(nums, i+1);
path.pop_back();
}
}
vector<vector<int> > getSubsets(vector<int> A, int n)
{
// write code here
sort(A.begin(), A.end(),[](int a,int b)
{
return a>b;
});
res.clear();path.clear();
recur(A, 0);
reverse(res.begin(), res.end());
return res;
}
};