78. 아이 집
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
int n=nums.size();
vector<vector<int>> res;
if(n==0) return res;
res.push_back({});
for(int i=0;i<n;i++){
int t=res.size();
for(int j=0;j<t;j++){
if(res[j].empty()) res.push_back({nums[i]});
else{
vector<int> tmp=res[j];
tmp.push_back(nums[i]);
res.push_back(tmp);
}
}
}
return res;
}
};
![](https://img-blog.csdnimg.cn/20200507000709606.png)
90. 아이 집 II
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> res;
vector<int>tmp;
func(res,nums,tmp,0);
return res;
}
void func(vector<vector<int>>&res,vector<int>& nums,vector<int> tmp,int k){
res.push_back(tmp);
for(int i=k;i<nums.size();i++){
if(i>k && nums[i]==nums[i-1]) continue;
tmp.push_back(nums[i]);
func(res,nums,tmp,i+1);
tmp.pop_back();
}
}
};
![](https://img-blog.csdnimg.cn/20200507004533722.png)