
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note
The solution set must not contain duplicate subsets.
Example
Input: [1,2,2]
Output:
1 2 3 4 5 6 7 8
|
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
|
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums); subsetsWithDupHelper(result, new ArrayList<>(), nums, 0); return result; }
private void (List<List<Integer>> result, List<Integer> subset, int[] nums, int idx) { result.add(new ArrayList<>(subset));
for (int i = idx; i < nums.length; i++) { if (i > idx && nums[i] == nums[i - 1]) continue;
subset.add(nums[i]); subsetsWithDupHelper(result, subset, nums, i + 1); subset.remove(subset.size() - 1); } }
|
近期评论