class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
nums.sort()
res = []
def dfs(pos, res, path):
for i in range(pos, len(nums)):
if i == pos or nums[i] != nums[i - 1]:
path.append(nums[i])
dfs(i + 1, res, path)
path.pop()
res.append([i for i in path])
dfs(0, res, [])
return res
class Solution {
private void helper(int[] nums, int start, List<Integer> path, List<List<Integer>> res) {
res.add(new ArrayList<>(path));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
path.add(nums[i]);
helper(nums, i + 1, path, res);
path.remove(path.size() - 1);
}
}
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
helper(nums, 0, new ArrayList<>(), res);
return res;
}
}