zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    一回生,二回熟
  • 相关阅读:
    几个函数小练习
    结构体和枚举类型
    结构体
    几个函数小练习
    函数简介
    网页端滚轮滑动事件
    碰撞检测
    Ajax前后台交互函数
    事件绑定与解除js
    移动端的屏幕适配问题
  • 原文地址:https://www.cnblogs.com/zzytxl/p/12682175.html
Copyright © 2011-2022 走看看