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

代码随想录算法训练营第22天 | 组合总和 分割回文串

39. 组合总和

39. 组合总和 - 力扣(LeetCode)

题目链接/文章讲解:代码随想录

视频讲解:带你学透回溯算法-组合总和(对应「leetcode」力扣题目:39.组合总和)| 回溯法精讲!_哔哩哔哩_bilibili

自己乱写的,回溯的时候没有去掉重复使用的数字

class Solution {
    List<Integer> group = new ArrayList<>();
    List<List<Integer>> result = new ArrayList<>();
    int sum = 0;
    public void backTracking (int[] candidates,int target){
        if(sum == target){
            List<Integer> temp = new ArrayList(group);
            Collections.sort(temp);
            for(List<Integer> oneList:result){
                if(oneList.equals(temp)==true)return;
            }
            result.add(new ArrayList(temp));
            return;
        }else if(sum > target)return;
        for(int i = 0;i < candidates.length;i++){
            group.add(candidates[i]);
            sum += candidates[i];
            backTracking(candidates,target);
            group.remove(group.size() - 1);
            sum -= candidates[i];
        }
    }
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backTracking(candidates,target);
        return result;
    }
}

没有剪枝

剪枝

修改

class Solution {
    List<Integer> group = new ArrayList<>();
    List<List<Integer>> result = new ArrayList<>();
    int sum = 0;
    public void backTracking (int[] candidates,int target,int startIndex){
        if(sum == target){
            result.add(new ArrayList(group));
            return;
        }
        for(int i = startIndex;i < candidates.length;i++){
            if(candidates[i] + sum > target)break;//排过序,现在已经超过target,说明后面的数已经不需要遍历了
            group.add(candidates[i]);
            sum += candidates[i];
            backTracking(candidates,target,i);
            group.remove(group.size() - 1);
            sum -= candidates[i];
        }
    }
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);//一定要先对数组排序,为了剪枝
        backTracking(candidates,target,0);
        return result;
    }
}

40.组合总和II

40. 组合总和 II - 力扣(LeetCode)

注意题目中给我们 集合是有重复元素的,那么求出来的 组合有可能重复,但题目要求不能有重复组合。

题目链接/文章讲解:代码随想录

视频讲解:​​​​​​回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II_哔哩哔哩_bilibili

class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> result = new ArrayList<>();
    public void backTracking(int[] candidates,int target,int startIndex,int sum,boolean[] used){
        if(sum == target){
            result.add(new ArrayList(path));
            return;
        }else if(sum > target)return;
        for(int i = startIndex;i < candidates.length;i++){
            if(sum + candidates[i] > target)break;//剪枝操作
            if(i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false){//去重操作
                continue;
            }
            path.add(candidates[i]);
            sum += candidates[i];
            used[i] = true;
            backTracking(candidates,target,i + 1,sum,used);//下一层递归
            path.remove(path.size() - 1);//回溯
            sum -= candidates[i];
            used[i] = false;
        }
    }
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        boolean[] used = new boolean[candidates.length];//记录元素是否被使用
        Arrays.sort(candidates);//必须排序
        backTracking(candidates,target,0,0,used);
        return result;
    }
}

131.分割回文串

131. 分割回文串 - 力扣(LeetCode)

本题较难,大家先看视频来理解 分割问题,明天还会有一道分割问题,先打打基础。

代码随想录

视频讲解:带你学透回溯算法-分割回文串(对应力扣题目:131.分割回文串)| 回溯法精讲!_哔哩哔哩_bilibili

class Solution {
    List<String> path = new ArrayList<>();
    List<List<String>> result = new ArrayList<>();
    public boolean huiwen(String s,int start,int end){
        while(start < end){
            if(s.charAt(start) != s.charAt(end))return false;
            start++;
            end--;
        }
        return true;
    }
    public void backTracking(String s,int startIndex){
        if(startIndex == s.length()){
            result.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex;i < s.length();i++){
            if(huiwen(s,startIndex,i)){//左闭右闭区间
                path.add(s.substring(startIndex,i + 1));//已经切过的不能再切
            }else continue;
            backTracking(s,i + 1);
            path.remove(path.size()-1);
        }
    }
    public List<List<String>> partition(String s) {
        backTracking(s,0);
        return result;
    } 
}

相关文章:

  • PyTorch 中的混合精度训练方法,从 autocast 到 GradScalar
  • Windows编程----进程:命令行参数
  • 如何根据应用需求选择光谱相机
  • OpenCV计算摄影学(18)平滑图像中的纹理区域同时保留边缘信息函数textureFlattening()
  • 第五章 起航16 申请一个外包资源
  • 【Linux 22.4 ubuntu 安装cuda12.1 完整方案】
  • OFA:通过简单的序列到序列学习框架统一架构、任务和模态
  • 数学建模:MATLAB强化学习
  • 通过 Docker openssl 容器生成生成Nginx证书文件
  • 2025-03-07 :详细介绍一下 Databricks 的 Lakehouse
  • 【华为OD机试真题29.9¥】(E卷,100分) - 运维日志排序(Java Python JS C++ C )
  • OpenHarmony子系统开发编译构建指导
  • dart中实现子isolate的管理类,特适用于针对数据库的处理
  • 【js逆向】iwencai国内某金融网站实战
  • 心智模式—系统思考
  • 小白学Agent技术[1]
  • 初阶数据结构(C语言实现)——3.4带头双向循环链表详解(定义、增、删、查、改)
  • Android AudioFlinger(四)—— 揭开PlaybackThread面纱
  • ollama 安装方式
  • 九章云极 Aladdin重塑 AI 开发范式的先锋力量
  • 济南市建设局网站/深圳百度推广公司
  • 做网站界面用什么软件/常州seo
  • 微软网站开发工具/管理人员课程培训
  • 建设官方网站企业网站/网络营销推广论文
  • 网站里的课程配图怎么做/黄石seo
  • 上海装修公司哪家最好/深圳外贸seo