90.子集2
给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的 子集(幂集)。
解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
示例 1:
1 2
   | 输入:nums = [1,2,2] 输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
   | 
 
示例 2:
1 2
   | 输入:nums = [0] 输出:[[],[0]]
   | 
 
Solution
答案视角:
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 32 33 34 35 36 37 38 39 40 41
   | public class Solution {          List<List<Integer>> result = new ArrayList<>();          List<Integer> path = new ArrayList<>();
           public List<List<Integer>> subsetsWithDup(int[] nums) {                  Arrays.sort(nums);                  dfs(nums, 0);         return result;     }
           private void dfs(int[] nums, int i) {                  result.add(new ArrayList<>(path));                  if (i == nums.length) {             return;         }
                   for (int j = i; j < nums.length; j++) {                          path.add(nums[j]);                          dfs(nums, j + 1);                          path.remove(path.size() - 1);                                       while (j < nums.length - 1 && nums[j] == nums[j + 1]) {                 ++j;             }         }     } }
 
  | 
 
选择视角
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 32 33 34 35 36 37 38 39 40 41
   |  public class Solution {          List<Integer> path = new ArrayList<>();          List<List<Integer>> ans = new ArrayList<>();
           public List<List<Integer>> subsetsWithDup(int[] nums) {                  Arrays.sort(nums);                  dfs(0, nums);         return ans;     }
           public void dfs(int i, int[] nums) {                  if (i == nums.length) {             ans.add(new ArrayList<>(path));             return;         }
                   path.add(nums[i]);                  dfs(i + 1, nums);                  path.remove(path.size() - 1);
                   while (i + 1 < nums.length && nums[i + 1] == nums[i]) {             i++;         }
                   dfs(i + 1, nums);     } }
 
 
  |