给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列

class Solution {
List<List<Integer>> res=new ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
boolean[] used;
public List<List<Integer>> subsetsWithDup(int[] nums) {
if(nums.length==0){
res.add(path);
return res;
}
Arrays.sort(nums);
used=new boolean[nums.length];
subHelp(nums,0);
return res;
}
private void subHelp(int[]nums,int startIndex){
res.add(new ArrayList<>(path));
if(startIndex>=nums.length){
return;
}
for(int i=startIndex;i<nums.length;i++){
if(i>0&&nums[i]==nums[i-1]&&!used[i-1]){
continue;
}
path.add(nums[i]);
used[i]=true;
subHelp(nums,i+1);
path.removeLast();
used[i]=false;
}
}
}
8955

被折叠的 条评论
为什么被折叠?



