首页 > 编程语言 >【代码随想录Day25】回溯算法Part04

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

时间:2024-09-24 19:50:03浏览次数:3  
标签:used 数字 nums Day25 随想录 Part04 int new path

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; // 回溯,标记当前数字为未使用
        }
    }
}

总结

总结:代码随想录

标签:used,数字,nums,Day25,随想录,Part04,int,new,path
From: https://blog.csdn.net/weixin_43724673/article/details/142466157

相关文章

  • 代码随想录算法训练营Day13 | 递归遍历、迭代遍历、层序遍历
    目录递归遍历和迭代遍历:144.二叉树的前序遍历94.二叉树的中序遍历145.二叉树的后序遍历层序遍历:102.二叉树的层序遍历107.二叉树的层序遍历Ⅱ199.二叉树的右视图637.二叉树的层平均值429.N叉树的层序遍历515.在每个树行中找最大值116.填充每个节点的下一个右侧......
  • Day 22 回溯法part04| LeetCode 491.递增子序列,46.全排列,47.全排列 II
    491.递增子序列491.非递减子序列classSolution{publicList<Integer>path=newLinkedList<>();publicList<List<Integer>>res=newArrayList<>();publicList<List<Integer>>findSubsequences(int[......
  • 【代码随想录Day24】回溯算法Part03
    93.复原IP地址题目链接/文章讲解:代码随想录视频讲解:回溯算法如何分割字符串并判断是合法IP?|LeetCode:93.复原IP地址_哔哩哔哩_bilibiliclassSolution{List<String>result=newArrayList<>();LinkedList<String>path=newLinkedList<>();publicL......
  • 代码随想录训练营第39天|树形dp
    198.打家劫舍classSolution{public:introb(vector<int>&nums){nums.insert(nums.begin(),0);intn=nums.size();vector<int>dp(n,INT_MIN);dp[0]=0;dp[1]=nums[1];for(inti=2;i<n;i++......
  • 【代码随想录Day23】回溯算法Part02
    39.组合总和题目链接/文章讲解:代码随想录视频讲解:带你学透回溯算法-组合总和(对应「leetcode」力扣题目:39.组合总和)|回溯法精讲!_哔哩哔哩_bilibiliclassSolution{//存储最终结果的列表List<List<Integer>>result=newArrayList<>();//存储当前路......
  • 代码随想录算法训练营第一天 | 209. 长度最小的子数组 59. 螺旋矩阵 58. 区间和 Java
    209.长度最小的子数组题目链接:https://leetcode.cn/problems/minimum-size-subarray-sum/description/解题思路思路1:暴力解法通过两个for循环,找出所有的可能的区间,并比较出最小区间思路2:滑动窗口因为需要找出的是连续的一个子数组,所以可以模拟一个从左到右滑动的一个......
  • 代码随想录算法 - 回溯3
    明天开始建模比赛,没时间写思路了题目193.复原IP地址有效IP地址正好由四个整数(每个整数位于0到255之间组成,且不能含有前导0),整数之间用'.'分隔。例如:"0.1.2.201"和"192.168.1.1"是有效IP地址,但是"0.011.255.245"、"192.168.1.312"和"[email protected]"是无......
  • 代码随想录算法训练营,9月20日 | 93.复原IP地址,78.子集,90.子集II
    93.复原IP地址题目链接:93.复原IP地址文档讲解︰代码随想录(programmercarl.com)视频讲解︰复原IP地址日期:2024-09-20Java代码如下:classSolution{List<String>res=newArrayList<>();privatevoidbackTracking(Strings,intstartIndex,intpointNum){......
  • 代码随想录训练营第38天|string虚拟头
    322.零钱兑换classSolution{public:intcoinChange(vector<int>&coins,intamount){vector<int>dp(amount+1,INT_MAX);dp[0]=0;for(auto&coin:coins){for(inti=1;i<=amount;i++){......
  • 代码随想录算法训练营第十六天 | Javascript | 力扣Leetcode | 回溯 | 77. 组合、216.
    目录前言简介题目链接:77.组合题目链接:216.组合总和3题目链接:17.电话号码的字母组合前言踏平坎坷成大道,斗罢艰险又出发!自律的尽头是自控,自控的尽头是硬控。愿道友们披荆斩棘,终能得偿所愿。简介本人是小几年经验的前端开发,算法基础只有力扣几十道题,非常薄......