var subsetsWithDup = function(nums) {
//先排序
nums = nums.sort(function(a, b){
return a - b;
});
let used = new Array(nums.length).fill(0);
let answers = [];
let answer = [];
function backtrack(index, start){
if(index <= nums.length)answers.push([...answer]);
for(let i = start; i < nums.length; i++){
//当之前的元素没有被使用时,说明和他相同的元素也不需要被使用
//只有当他前面的元素被用了时,他可能与他出现在同一个树枝上可以使用
if(i > 0 && nums[i] == nums[i - 1] && used[i - 1] == 0)continue;
answer.push(nums[i]);
used[i] = 1;
backtrack(index + 1, i + 1);
answer.pop();
used[i] = 0;
}
}
backtrack(0, 0);
return answers;
};
08-10
413
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)