算法笔记|Day20回溯算法II

算法笔记|Day20回溯算法II

  • [☆☆☆☆☆leetcode 39. 组合总和](#☆☆☆☆☆leetcode 39. 组合总和)
  • [☆☆☆☆☆leetcode 40.组合总和II](#☆☆☆☆☆leetcode 40.组合总和II)
  • [☆☆☆☆☆leetcode 131.分割回文串](#☆☆☆☆☆leetcode 131.分割回文串)

☆☆☆☆☆leetcode 39. 组合总和

题目链接:leetcode 39. 组合总和

题目分析

本题采用回溯算法,组合没有数量要求,且元素可无限重复选取,故每次遍历都可以从第一个元素开始。

代码

java 复制代码
class Solution {
    List<List<Integer>> res=new ArrayList<>();
    List<Integer> path=new LinkedList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtrcking(candidates,target,0,0);
        return res;
    }

    public void backtrcking(int candidates[],int target,int sum,int start){
        if(sum>target)
            return;
        if(sum==target){
            res.add(new ArrayList(path));
            return;
        }
        for(int i=start;i<candidates.length;i++){
            sum+=candidates[i];
            path.add(candidates[i]);
            backtrcking(candidates,target,sum,i);
            sum-=candidates[i];
            path.removeLast();
        }
    }
}

☆☆☆☆☆leetcode 40.组合总和II

题目链接:leetcode 40.组合总和II

题目分析

本题集合(数组candidates)有重复元素,但不能有重复的组合,涉及到去重的逻辑,采用了used数组,若该元素在本轮回溯遍历(树层)中用到过赋值为1,后续不再使用,回溯时恢复为0;但在递归遍历(树枝)中用到过,还可以继续使用。

代码

java 复制代码
class Solution {
    List<List<Integer>> res=new ArrayList<>();
    List<Integer> path=new LinkedList<>();

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        int used[]=new int[candidates.length];
        backtracking(candidates,target,0,0,used);
        return res;
    }

    public void backtracking(int candidates[],int target,int sum,int start,int used[]){
        if(sum>target)
            return;
        if(sum==target){
            res.add(new ArrayList(path));
            return;
        }
        for(int i=start;i<candidates.length;i++){
            if(i>0&&candidates[i]==candidates[i-1]&&used[i-1]==0)
                continue;
            sum+=candidates[i];
            path.add(candidates[i]);
            used[i]=1;
            backtracking(candidates,target,sum,i+1,used);
            sum-=candidates[i];
            path.removeLast();
            used[i]=0;
        }
    }
}

☆☆☆☆☆leetcode 131.分割回文串

题目链接:leetcode 131.分割回文串

题目分析

切割问题可以仿照组合问题利用回溯,从前往后搜索,如果发现回文,进入backtracking,起始位置后移一位,循环结束照例移除str的末位。

代码

java 复制代码
class Solution {
    List<List<String>> res=new ArrayList<>();
    List<String> str=new ArrayList<>();

    public List<List<String>> partition(String s) {
        backtracking(s,0,new StringBuilder());
        return res;
    }

    public void backtracking(String s,int start,StringBuilder sb){
        if(start==s.length()){
            res.add(new ArrayList(str));
            return;
        }
        for(int i=start;i<s.length();i++){
            sb.append(s.charAt(i));
            if(check(sb)){
                str.add(sb.toString());
                backtracking(s,i+1,new StringBuilder());
                str.removeLast();
            }
        }
    }

    public boolean check(StringBuilder sb){
        for(int i=0;i<sb.length()/2;i++){
            if(sb.charAt(i)!=sb.charAt(sb.length()-1-i))
                return false;
        }
        return true;
    }
}

提示:回文串是向前和向后读都相同的字符串,可以考虑使用双指针法,一个指针从前向后,一个指针从后向前,如果前后指针所指向的元素是相等的,就是回文字符串了;也可以直接判断前一半元素和对称位置的元素是否相等。

相关推荐
91刘仁德4 小时前
C++ 继承和多态 设计模式
c语言·c++·笔记
库玛西4 小时前
哈夫曼树与前缀编码:数据压缩的贪心核心
c语言·c++·笔记·考研
别动我齐刘海4 小时前
ROS2 Jazzy + C++ 实战路线——进阶学习3
c++·人工智能·vscode·python·算法·机器学习·机器人
zhangfeng11335 小时前
《从“人工适配“到“智能生成“:KernelSwift 跨国产芯片算子迁移全栈方案解读》 —— 强调范式跃迁和跨硬件属性,适合偏架构分析的写法
人工智能·算法·华为·ai编程·npu
0+1115 小时前
算法 --滑动窗口
c++·算法·leetcode
reikocao5 小时前
大场景下 Hessian 特征值放大与退化检测
算法
hetao17338375 小时前
2026-09-18 hetao1733837 的刷题记录
c++·算法
wuyk5556 小时前
21.计数排序:不用比较的“空间换时间”排序算法
python·算法·排序算法
Rabitebla6 小时前
【Linux系统编程】 指令(二):一条路径是怎么定位到文件的
linux·c++·笔记·学习·算法
库玛西6 小时前
数据结构与算法之美:树、森林与二叉树全解析
c语言·数据结构·笔记·考研·算法·学习方法