Given a set of distinct integers, nums, return all possible subsets (the power set).
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:
vector<vector<int>> subsets(vector<int>& nums) {
std::vector<std::vector<int> > result;
std::vector<int> item;
result.push_back(item);
generate(0, nums, item, result);
return result;
}
private:
void generate(int i, std::vector<int> &nums, std::vector<int> &item, std::vector<std::vector<int> > &result)
{
if(i>=nums.size())
{
return;
}
item.push_back(nums[i]);
result.push_back(item);
generate(i+1, nums, item, result);
item.pop_back();
generate(i+1, nums, item, result);
}
};