https://leetcode.cn/problems/subsets-ii/
class Solution {
ArrayList<List<Integer>> res=new ArrayList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);//先排序
boolean[] book=new boolean[nums.length];
res.add(new ArrayList<>());//[]
find(book,nums,0,new ArrayList<Integer>());
return res;
}
public void find(boolean[] book,int[] nums,int start,ArrayList<Integer> tmp){
if(start>=nums.length) return;
for(int i=start;i<nums.length;i++){
if(i-1>=0&&nums[i-1]==nums[i]){//前一个数等于它
if(!book[i-1]){//前一个数并没有在这条分支上被使用过
continue;//跳过这个数
}
}
book[i]=true;
tmp.add(nums[i]);
res.add(new ArrayList<>(tmp));//加入结果
find(book,nums,i+1,tmp);
tmp.remove(tmp.size()-1);
book[i]=false;
}
}
}
/*
每个元素在当前位置只能使用一次?
所有可能的子集:保存中间结果
*/