剪枝技巧前面有写过。加强印象
class Solution {
LinkedList<List<Integer>> ans=new LinkedList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
dfs(nums,0);
return ans;
}
void dfs(int[] nums,int cur){//cur是展开的多叉树的当前层
ans.add(new LinkedList(path));
for(int i=cur;i<nums.length;i++){//这循环是cur和它可以选择的下一个节点,这“下一个节点”在同一层
if(i>cur&&nums[i]==nums[i-1])
continue;
path.add(nums[i]);
dfs(nums,i+1);//不是cur+1!
path.removeLast();
}
}
}
原文:https://www.cnblogs.com/wsshub/p/14698441.html