90. 子集 II

给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

输入: [1,2,2]
输出:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> lists = new LinkedList<>();
        if(nums == null || nums.length == 0) return lists;
        Arrays.sort(nums);
        // boolean[] vi = new boolean[nums.length];
        bt(nums,0,new LinkedList<Integer>(),lists);
        return lists;
    }
    private void bt(int[] nums,int index,LinkedList<Integer> list,List<List<Integer>> lists){
        lists.add(new LinkedList<>(list));
        for(int i = index;i < nums.length;i++){
            // 注意: i > index
            if(i > index && nums[i] == nums[i - 1]) continue;
            list.add(nums[i]);
            bt(nums,i + 1,list,lists);
            list.removeLast();
        }
    }
}

 

posted @ 2020-04-11 20:56  海绵爱上星  阅读(95)  评论(0编辑  收藏  举报