90. Subsets II (Medium) Facebook
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.
For example,
If nums =[1,2,2]
, a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Solution 1: DFS O(n * 2^n); O(n)
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null) {
return res;
}
Arrays.sort(nums);
dfs(res, new ArrayList<>(), nums, 0);
return res;
}
private void dfs(List<List<Integer>> res, List<Integer> subset, int[] nums, int start) {
res.add(new ArrayList<>(subset));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
subset.add(nums[i]);
dfs(res, subset, nums, i + 1);
subset.remove(subset.size() - 1);
}
}
Follow Up:
- 第一题prime product(给你一组素数,求所有可能的乘积) , follow-up如果有重复怎么办