1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { List<Integer> path = new ArrayList<>(); List<List<Integer>> ans = new ArrayList<>(); int[] nums;
public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); this.nums = nums; dfs(false, 0); return new ArrayList<>(ans); }
private void dfs(boolean flag, int i) { if (i == nums.length) { ans.add(new ArrayList<>(path)); return; } dfs(false, i + 1);
if (!flag && i > 0 && nums[i - 1] == nums[i]) return; path.add(nums[i]); dfs(true, i + 1); path.remove(path.size() - 1); } }
|