从零学算法78

78 .给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

输入:nums = 1,2,3

输出:\[,1,2,1,2,3,1,3,2,3,1,2,3]

示例 2:

输入:nums = 0

输出:\[,0]

  • 如果将该题想象成一棵 n 叉树,进行 dfs,那么其实遍历时走过的每个路径都是子集。比如例子 1
复制代码
    		1
    	2		3
    3
  • 为了防止重复组合,所以在构建新的子集时,比如 1,3 我们继续构建时,不会往前取数字(取末尾的 3 前面的数字)构建成新的子集,比如得到 1,3,2,这也就是为什么上面那棵树是这样的。所以比如 1,2,3 ,我们得到 1,2,3 后不可能回溯成 1,3 然后得到 1,3,2,所以每次的下一层递归,从当前层递归的数组的下标 +1 开始,具体看代码
java 复制代码
  public List<List<Integer>> subsets(int[] nums) {
      List<List<Integer>> list = new ArrayList<>();
      backtrack(list, new ArrayList<>(), nums, 0);
      return list;
  }
  
  private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int start) {
      //走过的所有路径都是子集的一部分,所以都要加入到集合中
      list.add(new ArrayList<>(tempList));
      for (int i = start; i < nums.length; i++) {
          //做出选择
          tempList.add(nums[i]);
          //递归,i+1 防止重复结果
          backtrack(list, tempList, nums, i + 1);
          //撤销选择
          tempList.remove(tempList.size() - 1);
      }
  }
  • 位运算:其实在构建每个子集时都可以看成,每个数都有两种状态,加入子集或者不加入。设加入为 1,不加入为 0,数组长度为 n,就能把每个子集看做 n 位二进制数,那么总个数就是 2n。比如 1,2,3,每个子集都能看做三位的二进制数,\[\] 就是每个数都不选即 000,1,3 就对应 101。
  • 因此,比如例子 1,我们只需要遍历这 n(该例中为 8) 个数,范围为 0~7, 比如 6 对应为 110,就表示该子集为 2,3,再比如 3 对应为 011,表示子集 1,2(因为低位表示数组下标小的数)
java 复制代码
  public List<List<Integer>> subsets(int[] nums) {
      List<List<Integer>> res = new ArrayList<>();
      int len = nums.length;
      // 子集个数
      int resCount = 1<<len;
      // i 其实就是每种子集对应的二进制数的十进制
      for(int i=0;i<resCount;i++){
          List<Integer> list = new ArrayList<>();
          // 看是否包含 nums 中某个数
          for(int j=0;j<len;j++){
          	// 如果包含就添加到 list
              if((i>>j&1)==1)list.add(nums[j]);
          }
          res.add(list);
      }
      return res;
  }
  • 非递归:我们先加入一个空集,遍历时 nums 时每次都把 numsi 加到之前的子集中构建新子集,遍历完也就得到了全部子集。比如例子 1 我们会依次得到: []->[[],1]->[[],1,2,12]->[[],1,2,12,3,13,23,123]
java 复制代码
  public List<List<Integer>> subsets(int[] nums) {
      List<List<Integer>> res = new ArrayList<>();
      res.add(new ArrayList<>());
      for(int num : nums){
          int size = res.size();
          for(int j=0;j<size;j++){
              List<Integer> tempList = new ArrayList<>(res.get(j));
              tempList.add(num);
              res.add(tempList);
          }
      }
      return res;
  }
  • 强行递归也可以,其实就是递归形式的循环
java 复制代码
  public List<List<Integer>> subsets(int[] nums) {
      List<List<Integer>> res = new ArrayList<>();
      res.add(new ArrayList<>());
      dfs(nums,res,0);
      return res;
  }
  public void dfs(int[] nums,List<List<Integer>> res,int index){
      if(index >= nums.length)return;
      for(int i=0,j=res.size();i<j;i++){
          List<Integer> list = new ArrayList(res.get(i));
          list.add(nums[index]);
          res.add(list);
      }
      dfs(nums,res,index+1);
  }
  • 递归:还是上面的思路,我们在构建一种子集时,都可以看做对每个数选或不选。比如 nums:1,2,3 选择 12,不选 3,得到 011,也就是说完全可以当做一个二叉树,因为对于每个数只有选或不选两种可能,所以递归部分就写做选择的过程
java 复制代码
  public List<List<Integer>> subsets(int[] nums) {
      List<List<Integer>> res = new ArrayList<>();
      dfs(res, new ArrayList<>(), nums, 0);
      return res;
  }
  public void dfs(List<List<Integer>> res, List<Integer> list, int[] nums, int index){
      if(index == nums.length){
          res.add(new ArrayList<>(list));
          return;
      }
      // 不选,直接继续递归
      dfs(res, list, nums, index + 1);
      // 选择(加入 list)
      list.add(nums[index]);
      // 选完再递归
      dfs(res, list, nums, index + 1);
      // 撤销选择
      list.remove(list.size() - 1);
  }
相关推荐
CQU_JIAKE20 分钟前
8.5【A】
数据结构·算法
ESBK202533 分钟前
学术邀约|CMICA 2026 第二届计算方法、智能控制与航空航天国际会议
大数据·人工智能·算法·飞行器·智能控制·航空航天·计算
无bug代码搬运工44 分钟前
LeetCode 42 接雨水:双指针解法详解
算法·leetcode·职场和发展
Jasmine_llq1 小时前
《P13016 [GESP202506 六级] 最大因数》
数据结构·算法
白狐_7981 小时前
考研408算法设计题保命策略:链表专题暴力解法精讲(2015 & 2019真题实战)
考研·算法·链表
fangpin1 小时前
GPU编程2: 并行策略
算法
碧海银沙音频科技研究院2 小时前
8基于BES2700IHC数字助听器系统开发
嵌入式硬件·算法
HZZD_HZZD2 小时前
智能电表选CoAP还是MQTT?合众致达万表实测:CoAP省流17.3%、续航提升38%,附报文拆解与选型矩阵
算法
linux-hzh3 小时前
百日算法修炼 · Day 05
java·算法
paopaokaka_luck3 小时前
基于springboot3+vue3的云南本土影视文旅推荐平台(协同过滤算法、Echarts图形化分析)
前端·spring boot·学习·算法·echarts·mybatis