当前位置: 首页 > article >正文

【代码随想录Day25】回溯算法Part04

491.递增子序列

题目链接/文章讲解:代码随想录
视频讲解:回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列_哔哩哔哩_bilibili

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> findSubsequences(int[] nums) {
        backtracing(nums, 0);
        return result;
    }

    public void backtracing(int[] nums, int startIndex) {
        if (path.size() > 1) {
            result.add(new ArrayList<>(path));
        }

        // 使用 HashSet 来记录当前层级已经使用过的数字,避免重复
        Set<Integer> used = new HashSet<>();

        for (int i = startIndex; i < nums.length; i++) {
            // 如果当前数字已经在当前层级使用过,或者它小于 path 中的最后一个元素,跳过
            if (used.contains(nums[i]) || (!path.isEmpty() && nums[i] < path.getLast())) {
                continue;
            }

            // 标记当前数字为已使用
            used.add(nums[i]);
            path.add(nums[i]);
            backtracing(nums, i + 1);
            path.removeLast();
        }
    }
}

46.全排列

题目链接/文章讲解:代码随想录
视频讲解:组合与排列的区别,回溯算法求解的时候,有何不同?| LeetCode:46.全排列_哔哩哔哩_bilibili

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] used; // 用于记录哪些数字已经被使用过

    public List<List<Integer>> permute(int[] nums) {
        used = new boolean[nums.length]; // 初始化used数组
        backtracing(nums);
        return result;
    }

    public void backtracing(int[] nums) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path)); // 添加当前路径到结果集中
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i])
                continue; // 如果当前数字已经被使用过,跳过
            used[i] = true; // 标记当前数字为已使用
            path.add(nums[i]); // 将当前数字加入路径
            backtracing(nums); // 递归调用
            path.removeLast(); // 回溯,移除最后一个数字
            used[i] = false; // 回溯,标记当前数字为未使用
        }
    }
}

47.全排列 II

题目链接/文章讲解:代码随想录
视频讲解:回溯算法求解全排列,如何去重?| LeetCode:47.全排列 II_哔哩哔哩_bilibili

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    boolean[] used; // 用于记录哪些数字已经被使用过

    public List<List<Integer>> permuteUnique(int[] nums) {
        used = new boolean[nums.length]; // 初始化used数组
        Arrays.sort(nums);
        backtracing(nums);
        return result;
    }

    public void backtracing(int[] nums) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path)); // 添加当前路径到结果集中
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            // 如果当前数字已经被使用过,或者当前数字与前一个数字相同且前一个数字未被使用过,跳过
            if (used[i] || (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false)) {
                continue;
            }
            used[i] = true; // 标记当前数字为已使用
            path.add(nums[i]); // 将当前数字加入路径
            backtracing(nums); // 递归调用
            path.removeLast(); // 回溯,移除最后一个数字
            used[i] = false; // 回溯,标记当前数字为未使用
        }
    }
}

总结

总结:代码随想录


http://www.kler.cn/news/316578.html

相关文章:

  • CPU使用率较高排查和解决思路
  • vue + leaflet + 天地图实现搜索省份后高亮
  • Linux bash脚本本地开发环境(Git Bash)配置
  • 干货满满:嵌入式电阻的重要作用全知晓
  • 实验——完全使用Ansible部署多台服务器的服务
  • CTF 技能树 LOG -GIT泄露 笔记
  • 【磨皮美白】基于Matlab的人像磨皮美白处理算法,Matlab处理
  • react hooks--useContext
  • 【Nginx】在 Docker 上安装 Nginx 的详细指南
  • CVE-2024-46103
  • 无源蜂鸣器简介
  • 【百日算法计划】:每日一题,见证成长(017)
  • Python 类class的用法详解
  • 渗透测试综合靶场 DC-2 通关详解
  • comfyui中报错 Cmd(‘git‘) failed due to: exit code(128) 如何解决
  • 【医疗大数据】医疗保健领域的大数据管理:采用挑战和影响
  • 【2025】中医药健康管理小程序(安卓原生开发+用户+管理员)
  • 计算机毕业设计推荐-基于python的白酒销售数据可视化分析
  • 828华为云征文 | 构建高效搜索解决方案,Elasticsearch Kibana的完美结合
  • 计算结构力学,平行桁架杆件轴力计算源程序
  • Spring IoC 注解 总结
  • vue是如何优化
  • 【C++算法】分治——快排
  • 力扣(leetcode)每日一题 2374 边积分最高的节点
  • 神经生物学精解【2】
  • LeetCode[中等]
  • 迷雾大陆免费辅助:强流派推荐攻略!VMOS云手机自动辅助挂机教程!
  • [JavaEE] TCP协议
  • hql杂谈一
  • 黑马智数Day3