题目要求:
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2]
, a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
没有bugfree, 主要原因:有些小的字母打错
主要是使用DFS进行处理, 但是其中有一些问题是例如: 进行代码去重的问题
public class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> resultSet = new ArrayList<>();
List<Integer> list = new ArrayList<>();
if (nums == null || nums.length == 0) {
return resultSet;
}
Arrays.sort(nums);
utilRecursion(resultSet, list, nums, 0);
return resultSet;
}
private void utilRecursion(List<List<Integer>> resultSet, List<Integer> list, int[] nums, int pop) {
resultSet.add(new ArrayList<>(list));
for(int i = pop; i < nums.length; i++) {
if (i != 0 && nums[i] == nums[i - 1] && i > pop) {
continue;
}
list.add(nums[i]);
utilRecursion(resultSet, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}