Description
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
Example
If nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
思路
- 常规的回溯
代码
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
vector<int> vec;
getResult(res, vec, nums, 0);
return res;
}
void getResult(vector<vector<int>>& res, vector<int>&vec,
vector<int>& nums, int t){
res.push_back(vec);
for(int i = t; i < nums.size(); ++i){
vec.push_back(nums[i]);
getResult(res, vec, nums, i + 1);
vec.pop_back();
}
}
};