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

news2025/3/9 15:38:01

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;
    } 
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2312205.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

DeepSeek 医疗大模型微调实战讨论版(第一部分)

DeepSeek医疗大模型微调实战指南第一部分 DeepSeek 作为一款具有独特优势的大模型,在医疗领域展现出了巨大的应用潜力。它采用了先进的混合专家架构(MoE),能够根据输入数据的特性选择性激活部分专家,避免了不必要的计算,极大地提高了计算效率和模型精度 。这种架构使得 …

lvgl在ubuntu中模拟运行

文章目录 前言具体的步骤 前言 lvgl是一个图像UI的开源框架&#xff0c;用于嵌入式的设备之中。 在学习lvgl时&#xff0c;我们最好是现在PC上模拟运行&#xff0c;所以我们学习lvgl的第一步可以说是在我们的电脑上搭建模拟的运行环境。 参考官方的操作 lvgl在ubuntu上模拟运…

Unity引擎使用HybridCLR(华佗)热更新

大家好&#xff0c;我是阿赵。   阿赵我做手机游戏已经有十几年时间了。记得刚开始从做页游的公司转到去做手游的公司&#xff0c;在面试的时候很重要的一个点&#xff0c;就是会不会用Lua。使用Lua的原因很简单&#xff0c;就是为了热更新。   热更新游戏内容很重要。如果…

Vue项目通过内嵌iframe访问另一个vue页面,获取token适配后端鉴权(以内嵌若依项目举例)

1. 改造子Vue项目进行适配(ruoyi举例) (1) 在路由文件添加需要被外链的vue页面配置 // 若依项目的话是 router/index.js文件 {path: /contrast,component: () > import(/views/contrast/index),hidden: true },(2) 开放白名单 // 若依项目的话是 permission.js 文件 cons…

vue3 vite项目安装eslint

npm install eslint -D 安装eslint库 npx eslint --init 初始化配置&#xff0c;按项目实际情况选 自动生成eslint.config.js&#xff0c;可以添加自定义rules 安装ESLint插件 此时打开vue文件就会标红有问题的位置 安装prettier npm install prettier eslint-config-pr…

Excel·VBA江西省预算一体化工资表一键处理

每月制作工资表导出为Excel后都需要调整格式&#xff0c;删除0数据的列、对工资表项目进行排序、打印设置等等&#xff0c;有些单位还分有“行政”、“事业”2个工资表就需要操作2次。显然&#xff0c;这种重复操作的问题&#xff0c;可以使用VBA代码解决 目录 代码使用说明1&a…

【A2DP】SBC 编解码器互操作性要求详解

目录 一、SBC编解码器互操作性概述 二、编解码器特定信息元素(Codec Specific Information Elements) 2.1 采样频率(Sampling Frequency) 2.2 声道模式(Channel Mode) 2.3 块长度(Block Length) 2.4 子带数量(Subbands) 2.5 分配方法(Allocation Method) 2…

R软件线性模型与lmer混合效应模型对生态学龙类智力测试数据层级结构应用

全文链接&#xff1a;https://tecdat.cn/?p40925 在生态与生物学研究中&#xff0c;数据常呈现复杂结构特征。例如不同种群、采样点或时间序列的观测数据间往往存在相关性&#xff08;点击文末“阅读原文”获取完整代码、数据、文档&#xff09;。 传统线性模型在处理这类非独…

打造智能聊天体验:前端集成 DeepSeek AI 助你快速上手

DeepSeek AI 聊天助手集成指南 先看完整效果&#xff1a; PixPin_2025-02-19_09-15-59 效果图&#xff1a; 目录 项目概述功能特点环境准备项目结构组件详解 ChatContainerChatInputMessageBubbleTypeWriter 核心代码示例使用指南常见问题 项目概述 基于 Vue 3 TypeScrip…

C语言-语法

数据类型 字符串 C中字符串拼接不用+号,直接使用空格。 char* str = "hello" "world"; 换行链接,加上\就不会报错 char* longStr = "00000000000000000000000000000\ 00000000000000000000000000000"; typedef C 语言提供了 typedef …

Unity组件TrailRenderer屏幕滑动拖尾

Unity组件TrailRenderer屏幕滑动拖尾 介绍制作总结 介绍 今天要做一个拖动效果&#xff0c;正好用到了TrailRenderer这个组件&#xff0c;正好分享一下 效果参考如下&#xff1a; 制作 1.创建空物体TrailObject添加组件TrailRenderer 下面的材质可以根据自己想要制作的效果去…

大模型信息整理

1. Benchmarks Reasoning, conversation, Q&A benchmarks HellaSwagBIG-Bench HardSQuADIFEvalMuSRMMLU-PROMT-BenchDomain-specific benchmarks GPQAMedQAPubMedQAMath benchmarks GSM8KMATHMathEvalSecurity-related benchmarks PyRITPurple Llama CyberSecEval2. 国内外…

【Tools】Windows下Git 2.48安装教程详解

00. 目录 文章目录 00. 目录01. Git简介02. Git参考资料03. Git安装04. Git测试05. 附录 01. Git简介 Git(读音为/gɪt/。)是一个开源的分布式版本控制系统&#xff0c;可以有效、高速的处理从很小到非常大的项目版本管理。 [1] Git 是 Linus Torvalds 为了帮助管理 Linux 内核…

护网中shiro常问的问题

1. 漏洞原理 Apache Shiro 是一个强大的 Java 安全框架&#xff0c;提供身份验证、授权、加密及会话管理功能。Shiro 使用 rememberMe 机制来存储用户会话信息&#xff0c;该机制依赖于加密后的 Cookie。当攻击者能够控制 Cookie 并且服务器使用了不安全的反序列化机制时&…

swift -(5) 汇编分析结构体、类的内存布局

一、结构体 在 Swift 标准库中&#xff0c;绝大多数的公开类型都是结构体&#xff0c;而枚举和类只占很小一部分 比如Bool、 Int、 Double、 String、 Array、 Dictionary等常见类型都是结构体 ① struct Date { ② var year: Int ③ var month: Int ④ …

软件工程笔记下

从程序到软件☆ 章节 知识点 概论☆ 软件的定义&#xff0c;特点&#xff0c;生存周期。软件工程的概论。软件危机。 1.☆软件&#xff1a;软件程序数据文档 &#xff08;1&#xff09;软件&#xff1a;是指在计算机系统的支持下&#xff0c;能够完成特定功能与性能的包括…

【项目日记(九)】细节优化与对比测试

前言 上面我们对申请和释放的过程都已写完&#xff0c;并进行了单线程的联调。本期我们来对一些细节进行优化以及与malloc 进行对比测试。 目录 前言 一、大于256KB的内存申请问题 • 申请过程 • 释放过程 • 简单测试 二、使用定长内存池脱离使用new 三、优化释放对…

PyTorch系列教程:编写高效模型训练流程

当使用PyTorch开发机器学习模型时&#xff0c;建立一个有效的训练循环是至关重要的。这个过程包括组织和执行对数据、参数和计算资源的操作序列。让我们深入了解关键组件&#xff0c;并演示如何构建一个精细的训练循环流程&#xff0c;有效地处理数据处理&#xff0c;向前和向后…

10 【HarmonyOS NEXT】 仿uv-ui组件开发之Avatar头像组件开发教程(一)

温馨提示&#xff1a;本篇博客的详细代码已发布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下载运行哦&#xff01; 目录 第一篇&#xff1a;Avatar 组件基础概念与设计1. 组件概述2. 接口设计2.1 形状类型定义2.2 尺寸类型定义2.3 组件属性接口 3. 设计原则4. 使用…

C语言——【全局变量和局部变量】

&#x1f680;个人主页&#xff1a;fasdfdaslsfadasdadf &#x1f4d6;收入专栏&#xff1a;C语言 &#x1f30d;文章目入 1.&#x1f680; 全局变量2.&#x1f680; 局部变量3.&#x1f680; 局部和全局变量&#xff0c;名字相同呢? 1.&#x1f680; 全局变量 全局变量&…