子集
题目链接:子集
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
方法一:回溯
class Solution {
public:
vector<vector<int>> ans;
vector<int> t;
void f(vector<int>& nums,int x) {
if (x >= nums.size()) {
ans.push_back(t);
return ;
}
t.push_back(nums[x]);
f(nums,x+1);
t.pop_back();
f(nums,x+1);
}
vector<vector<int>> subsets(vector<int>& nums) {
f(nums,0);
return ans;
}
};