class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
backtracking(nums, 0);
return res;
}
public void backtracking(int[] nums, int startIndex){
res.add(new ArrayList<>(path));
if(startIndex == nums.length) return;
for(int i = startIndex; i < nums.length;i++){
if(i > startIndex && nums[i] == nums[i - 1]) continue;
path.add(nums[i]);
backtracking(nums, i + 1);
path.removeLast();
}
}
}
类似排列组合II,要进行树层去重,首先要进行对nums排序。