ID | Title | Difficulty | |
---|---|---|---|
Loading... |
90. Subsets II
Medium
LeetCode
Array, Backtracking, Bit Manipulation
Problem
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:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Code
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length == 0) return res;
Arrays.sort(nums);
helper(res, nums, 0, new ArrayList<>());
return res;
}
private void helper(List<List<Integer>> res, int[] nums, int index, List<Integer> temp){
res.add(new ArrayList<>(temp));
for(int i = index; i < nums.length; i++){
if(i != index && nums[i] == nums[i - 1]) continue;
temp.add(nums[i]);
helper(res, nums, i + 1, temp);
temp.remove(temp.size() - 1);
}
}
}
按 <- 键看上一题!
89. Gray Code
按 -> 键看下一题!
91. Decode Ways