腾讯//子集
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例:
输入: nums = [1,2,3] 输出: [ [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> out;
sort(nums.begin(), nums.end());
getSubsets(nums,0,out,res);
return res;
}
void getSubsets(vector<int> &nums,int pos,vector<int> &out,vector<vector<int>> &res){
res.push_back(out);
for(int i = pos; i < nums.size(); i++){
out.push_back(nums[i]);
getSubsets(nums,i+1,out,res);
out.pop_back();
}
}
};
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res(1);
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i++){
int size = res.size();
for(int j = 0; j < size; j++){
res.push_back(res[j]);
res.back().push_back(nums[i]);
}
}
return res;
}
};