题解 | #集合的所有子集(一)#
集合的所有子集(一)
http://www.nowcoder.com/practice/c333d551eb6243e0b4d92e37a06fbfc9
回溯法
class Solution {
public:
vector<vector<int>> ret;
vector<vector<int>> subsets(vector<int>& nums) {
vector<int> path;
backTracking(nums, path, 0);
return ret;
}
void backTracking(vector<int>& nums, vector<int>& path, int idx)
{
ret.push_back(path);
if(idx == nums.size())
{
return;
}
for(int i = idx;i < nums.size();i++)
{
path.push_back(nums[i]);
backTracking(nums, path, i+1);
path.pop_back();
}
}
};