【子集回溯】Leetcode 78. 子集 90. 子集 II

【子集回溯】Leetcode 78. 子集 90. 子集 II

---------------🎈🎈78. 子集 题目链接🎈🎈-------------------
在这里插入图片描述

78. 子集

class Solution {
    List<List<Integer>> result= new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        result.add(new ArrayList<>());
        helper(nums,0);
        return result;
    }
    List<Integer> resulttemp = new ArrayList<>();
    public void helper(int[] nums, int start){
        for(int i = start; i < nums.length; i++){
            resulttemp.add(nums[i]);
            result.add(new ArrayList<>(resulttemp));
            helper(nums,i+1);
            resulttemp.removeLast();
        }
    }
}
        

在这里插入图片描述

---------------🎈🎈90. 子集 II 题目链接🎈🎈-------------------

90. 子集 II

使用一个flag 树层去重——当前层如果遇到相同的就跳过 【去重前记得排序!】

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> resulttemp = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        result.add(resulttemp);
        helper(nums,0);
        return result;
    }
    public void helper(int[] nums, int start){
        boolean flag = false;
        for(int i = start; i < nums.length; i++){
            if(flag && nums[i] == nums[i-1] ){
                continue;
            }
            flag = true;
            resulttemp.add(nums[i]);
            result.add(new ArrayList<>(resulttemp));
            helper(nums,i+1);
            resulttemp.removeLast();

        }
    }
}

相关推荐

  1. day28回溯算法part04| 93.复原IP地址 78.子集 90.子集II

    2024-04-04 14:32:03       38 阅读
  2. 78.子集&90.子集2

    2024-04-04 14:32:03       45 阅读
  3. 回溯Leetcode 78. 子集【中等】

    2024-04-04 14:32:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-04 14:32:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-04 14:32:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-04 14:32:03       82 阅读
  4. Python语言-面向对象

    2024-04-04 14:32:03       91 阅读

热门阅读

  1. 七、c++代码中的安全风险-strcpy

    2024-04-04 14:32:03       39 阅读
  2. 黑客常用端口利用总结

    2024-04-04 14:32:03       32 阅读
  3. Spring MVC 的返回值有哪些

    2024-04-04 14:32:03       37 阅读
  4. MySQL之事务相关详细总结

    2024-04-04 14:32:03       37 阅读
  5. 金融应用出海市场与营销洞察

    2024-04-04 14:32:03       34 阅读
  6. I.MX6ULL的MAC网络外设设备树实现说明二

    2024-04-04 14:32:03       37 阅读
  7. ubuntu 使用 apt 安装、卸载 mysql

    2024-04-04 14:32:03       33 阅读
  8. PyQt6猜数字小游戏

    2024-04-04 14:32:03       38 阅读