题解:
本题在子集问题的基础上增加了去重
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void backtracking(vector<int>& nums,int startIndex){
if(startIndex == nums.size()){
return;
}
//单层搜索过程
for(int i = startIndex;i<nums.size();i++){
if(i>startIndex && nums[i] == nums[i-1]){
//该数已经取过 同一树层去重 不再取
continue;
}
path.push_back(nums[i]);
result.push_back(path);
backtracking(nums,i+1);
path.pop_back();
}
return ;
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
//排序 便于后续去重操作
sort(nums.begin(),nums.end());
//空集加上
result.push_back(path);
backtracking(nums,0);
return result;
}
};