day23 | 39. 组合总和 40.组合总和II 131.分割回文串 Leetcode 39. 组合总和

embedded/2024/10/22 16:22:36/

代码随想录算法训练营第23 天| 39. 组合总和 40.组合总和II 131.分割回文串

Leetcode 39. 组合总和
leetcodecnproblemscombinationsumdescription_4">题目链接:https://leetcode.cn/problems/combination-sum/description/
题目描述:

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入: candidates = [2], target = 1
输出: []

提示:

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • candidates 的所有元素 互不相同
  • 1 <= target <= 40
思路:

1、回溯

2、剪枝

图示:

在这里插入图片描述

代码1:回溯
class Solution {// 声明结果和单一路径List<List<Integer>> res = new ArrayList<>();LinkedList<Integer> path = new LinkedList<>();public List<List<Integer>> combinationSum(int[] candidates, int target) {backTracking(candidates , target , 0 , 0);return res;}public void backTracking(int[] candidates, int target , int sum , int startIndex){// 终止条件if(target < sum){return;}if(target == sum){res.add(new ArrayList<>(path));return;}// 单层递归逻辑for(int i = startIndex ; i < candidates.length ; i++){path.add(candidates[i]);sum += candidates[i];backTracking(candidates , target , sum , i);path.removeLast();sum -= candidates[i];}}
}
代码2:剪枝
class Solution {// 剪枝// 如果知道下一层的sum > target ,那么就没有必要进入下一层递归了// 声明结果和单一路径List<List<Integer>> res = new ArrayList<>();LinkedList<Integer> path = new LinkedList<>();public List<List<Integer>> combinationSum(int[] candidates, int target) {// 先将数组进行排序Arrays.sort(candidates);bakcTracking(candidates , target , 0 , 0);return res;}public void bakcTracking(int[] candidates , int target , int sum , int startIndex){// 终止条件// 找到数字和为target的组合if(sum == target){res.add(new ArrayList<>(path));return;}for(int i = startIndex ; i < candidates.length ; i++){// 如果sum > target , 终止遍历if(sum + candidates[i] > target) {break;}path.add(candidates[i]);sum += candidates[i];bakcTracking(candidates , target , sum , i);path.remove(path.size() - 1);sum -= candidates[i];}}
}
总结:
Leetcode 40.组合总和II
leetcodecnproblemscombinationsumiidescription_142">题目链接:https://leetcode.cn/problems/combination-sum-ii/description/
题目描述:

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次

**注意:**解集不能包含重复的组合。

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]

提示:

  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30
思路:

1、回溯

图示:

在这里插入图片描述

代码1:回溯
class Solution {List<List<Integer>> res = new ArrayList<>();LinkedList<Integer> path = new LinkedList<>();boolean[] used;public List<List<Integer>> combinationSum2(int[] candidates, int target) {// 将标志数组,用于树层去重used = new boolean[candidates.length];Arrays.sort(candidates);backTracking(candidates , target , 0 , 0);return res;}public void backTracking(int[] candidates , int target , int sum , int startIndex){// 终止条件if(sum > target){return ;}if(sum == target){res.add(new ArrayList<>(path));return ;}// 单层搜索逻辑for(int i = startIndex ; i < candidates.length ; i++){// 跳过同一树层使用过的元素if( i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false){continue;}used[i] = true;sum += candidates[i];path.add(candidates[i]);backTracking(candidates , target , sum , i + 1);sum -= candidates[i];used[i] = false;path.removeLast();}}
}
Leetcode 131.分割回文串
leetcodecnproblemspalindromepartitioningdescription_243">题目链接:https://leetcode.cn/problems/palindrome-partitioning/description/
题目描述:

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是

回文串

。返回 s 所有可能的分割方案。

示例 1:

输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]

示例 2:

输入:s = "a"
输出:[["a"]]

提示:

  • 1 <= s.length <= 16
  • s 仅由小写英文字母组成
思路:

1、回溯

图示:

在这里插入图片描述

代码1:回溯
class Solution {List<List<String>> res = new ArrayList<>();Deque<String> path = new LinkedList<>();public List<List<String>> partition(String s) {backTracking(s , 0);return res;}public void backTracking(String s , int startIndex ){//startIndex代表切割线 // 终止条件if(startIndex >= s.length()){res.add(new ArrayList(path));return;}// 单层逻辑for(int i = startIndex ; i < s.length() ; i++){if(isVaild(s , startIndex , i)){// 是回文子串// 获取回文子串String str = s.substring(startIndex , i  + 1);path.add(str);}else{continue;}// 寻找起始位置为i + 1 的子串backTracking(s , i + 1);// 回溯过程,弹出已经处理的子串path.removeLast();}}// 判断回文public boolean isVaild(String s ,int start ,int end){for(int i = start , j = end ; i < j ; i++ , j--){if(s.charAt(i) != s.charAt(j)){return false;}}return true;}
}

http://www.ppmy.cn/embedded/86962.html

相关文章

CI/CD学习之路

CI/CD之路 https://mp.weixin.qq.com/mp/appmsgalbum?__bizMzg4NDg0MjQ0MQ&actiongetalbum&album_id3433192036428447744&scene173&subscene&sessionidsvr_0b1e7fe1d6b&enterid1721879847&from_msgid2247485821&from_itemidx1&count3&am…

基于OpenLCA、GREET、R语言的生命周期评价方法、模型构建及典型案例应用

生命周期分析 (Life Cycle Analysis, LCA) 是评价一个产品系统生命周期整个阶段——从原材料的提取和加工&#xff0c;到产品生产、包装、市场营销、使用、再使用和产品维护&#xff0c;直至再循环和最终废物处置——的环境影响的工具。这种方法被认为是一种“从摇篮到坟墓”的…

python 图片转文字、语音转文字、文字转语音保存音频并朗读

一、python图片转文字 1、引言 pytesseract是基于Python的OCR工具&#xff0c; 底层使用的是Google的Tesseract-OCR 引擎&#xff0c;支持识别图片中的文字&#xff0c;支持jpeg, png, gif, bmp, tiff等图片格式 2、环境配置 python3.6PIL库安装Google Tesseract OCR 3、安…

linux离线安装mysql8(单机版)

文章目录 一、检查服务器是否有残留mysql资源&#xff0c;有的话就全删除1.1、查询mysql已安装的相关依赖&#xff1a;1.2、查找含有MySQL的目录 二、安装2.1、上传mysql安装包到文件夹下并解压2.2、移动及重命名2.3、mysql用户2.4、配置mysql所需的my.cnf文件2.5、给my.cnf配置…

Web前端:HTML篇(四)头部head标签与样式表的导入

头部<head> <head> 元素包含了所有的头部标签元素。在 <head>元素中你可以插入脚本&#xff08;scripts&#xff09;, 样式文件&#xff08;CSS&#xff09;&#xff0c;及提供元信息&#xff08;各种meta信息&#xff09;。 可以添加在头部区域的元素标签…

【WEB安全】 PHP基础完整教学上(超详细)

文章目录 1.PHP简述 php的使用&#xff1a; 2.基本语法格式 变量的命名 3.数据类型、常量以及字符串 预定义常量 整型 字符串&#xff1a;字符串变量用于存储并处理文本。 基本运算符&#xff1a; 5.控制语句 条件控制语句 循环控制语句 6.php数组 常用的数组函数…

Hive3:Hive初体验

1、创建表 CREATE TABLE test(id INT, name STRING, gender STRING);2、新增数据 INSERT INTO test VALUES(1, 王力红, 男); INSERT INTO test VALUES(2, 钉钉盯, 女); INSERT INTO test VALUES(3, 咔咔咔, 女);3、查询数据 简单查询 select * from test;带聚合函数的查询 …

【Docker】搭建实用的内网穿透工具 - FRP

前言 本教程基于群晖的NAS设备DS423+的docker功能进行搭建FRP的客户端,DSM版本为 7.2.1-69057 Update 5。采用香港机Debian 12系统的服务器来安装FRP的服务端作为演示。 服务器购买地址:https://www.crash.work/aff/AQXGDNKY 简介 FRP(Fast Reverse Proxy)是一个高性能…