90. 子集 II
Difficulty: 中等
给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
**说明:**解集不能包含重复的子集。
示例:
输入: [1,2,2]
输出:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Solution
Language: ****
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
if(nums.size() <=0) return {
};
sort (nums.begin() , nums.end() );
vector<vector<int>> res;
//有序数组
vector<int>_t;
res.push_back({
});
dfs(res,nums,0, _t);
return res;
}
void dfs(vector<vector<int>> &res, vector<int>&nums,int index, vector<int>& _temp ) {
if (index >= nums.size() ) {
return;
}
for (int i = index; i<nums.size() ;++i) {
if (i>index && nums[i-1] == nums[i]) continue;
_temp.push_back(nums[i]);
vector _res = _temp;
//复制 走过的路径
res.push_back(_res);
dfs(res,nums,i+1, _temp);
_temp.pop_back();
}
}
};