算法笔记|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;
    }
}

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

相关推荐
电科_银尘6 分钟前
【大模型算法与工程 56】Tool Calling:工具描述、结构化参数与返回协议
jvm·算法·oracle
叼烟扛炮17 分钟前
[LangGraph] 学习笔记(047):Pydantic 写的 State 也不校验,并行分支还互相看不见
笔记·学习
hai31524754319 分钟前
阶乘分层关系模型存储文本与表格
数据结构·算法·rust·排序算法·线性回归
小白的高手之路20 分钟前
【LeetCode 热题 100】206. 反转链表|链表、迭代、递归(Python)
笔记·python·算法·leetcode·链表·职场和发展·力扣
Regulus-Moon29 分钟前
PageRank:把整个 Web 变成一条马尔可夫链——平稳分布、谱收敛与 d=0.85 的定价
算法·搜索引擎·推荐算法
leo_厉锵43 分钟前
JMeter 性能测试实操笔记
笔记·jmeter
凢en1 小时前
AMD-SAM(Resizable BAR)
笔记·嵌入式硬件·gpu算力
凌冰_1 小时前
数据结构 - 数组(八)
数据结构·算法·矩阵
编程界的思想家1 小时前
君子爱财 取之有道
经验分享·笔记
16月6日-晴1 小时前
122. 买卖股票的最佳时机 II【贪心】
算法·leetcode