题目链接
回溯
class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
boolean[] used;
public List<List<Integer>> subsetsWithDup(int[] nums) {
used = new boolean[nums.length];
Arrays.fill(used, false);
Arrays.sort(nums);
backtracking(nums, 0);
return res;
}
public void backtracking(int[] nums, int startIndex){
// 求全部子集
res.add(new ArrayList<>(list));
for(int i = startIndex; i < nums.length; i++){
// 去重
if(i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false){
continue;
}
used[i] = true;
list.add(nums[i]);
backtracking(nums, i + 1);
used[i] = false;
list.removeLast();
}
}
}
本题是子集问题+组合求和Ⅲ的综合