解决思路
回溯+剪枝
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0)
return res;
//先排序
Arrays.sort(nums);
backtrack(0,nums,res,new ArrayDeque<>());
return res;
}
private void backtrack(int begin,int[] nums,List<List<Integer>> res,Deque<Integer> temp){
res.add(new ArrayList<>(temp));
for (int i = begin;i < nums.length;i++){
//剪枝
if (i > begin && nums[i] == nums[i - 1])
continue;
temp.addLast(nums[i]);
backtrack(i + 1,nums,res,temp);
temp.removeLast();
}
}
}