代码随想录Day20

server/2025/3/31 18:05:46/

Day20

回溯算法part02

LeetCode 39. 组合总和

题目描述

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

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

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

示例

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

题目链接

https://leetcode.cn/problems/combination-sum/

思路

在原有的组合问题上,将startIndex的递归值变为i确保每次元素能够重复使用

解决代码
java">class Solution {List<List<Integer>> result = new ArrayList<>();List<Integer> path = new ArrayList<>();int sum = 0;public List<List<Integer>> combinationSum(int[] candidates, int target) {Arrays.sort(candidates);//排序backTracking(candidates, target, 0);return result;}public void backTracking(int[] candidates, int target, int startIndex){if(sum > target){return;}if(sum == target){result.add(new ArrayList<>(path));return;}for(int i = startIndex; i < candidates.length; i++){path.add(candidates[i]);sum += candidates[i];backTracking(candidates, target, i);//递归起始位置为i,确保能重复使用元素sum -= candidates[i];path.removeLast();}}
}

LeetCode 40.组合总和II

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

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

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

示例

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

题目链接

https://leetcode.cn/problems/combination-sum-ii/description/

思路

和上一题的关键在于, 确保每层每个元素只遍历一次。涉及到回溯的, 在草稿纸上画图推导过程比较好

解决代码
java">class Solution {List<List<Integer>> result = new ArrayList<>();List<Integer> path = new ArrayList<>();int sum = 0;public List<List<Integer>> combinationSum2(int[] candidates, int target) {        Arrays.sort(candidates);backTracking(candidates, target, 0);return result;}public void backTracking(int[] candidates, int target, int startIndex){ if(sum == target){//找到目标路径result.add(new ArrayList(path));return;}for(int i = startIndex; i < candidates.length; i++){if(sum > target){break;}//确保每层,每个元素只用一次if(i > startIndex && candidates[i] == candidates[i - 1]){continue;}path.add(candidates[i]);sum += candidates[i];backTracking(candidates, target, i + 1);sum -= candidates[i];path.removeLast();}}
}

LeetCode 131.分割回文串

题目描述

给你一个字符串 s,请你将 s 分割成一些 子串,使每个子串都是 。返回 s 所有可能的分割方案。

示例

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

题目链接

https://leetcode.cn/problems/palindrome-partitioning/description/

思路

这道题可以用隔板法, 每次对字符串放一个隔板(假想)进行一分为二。如果前面的字符串是回文数, 则进行下一层的递归。否则将下一个字符拼接到当前隔板前面的子串后。

递归的终止条件,即隔板位置到字符串最末端。

解决代码
java">class Solution {List<List<String>> result = new ArrayList<>();//存储结果集List<String> path = new ArrayList<>();//存储路径public List<List<String>> partition(String s) {backTrack(s, 0, new StringBuilder());return result;}/*** @param s 待操作字符串* @param startIndex 当前隔板的字符串下标* @param sb 负责拼接字符串*/public void backTrack(String s, int startIndex, StringBuilder sb){if(startIndex == s.length()){//所有隔板已经放完,存入结果集result.add(new ArrayList(path));return;}for(int i = startIndex; i < s.length(); i++){sb.append(s.charAt(i));if(check(sb)){//如果目前已分割的字符串是回文数,执行下一步,否则,隔板放在下一个,拼接下一个字符串到sb中path.add(sb.toString());backTrack(s, i + 1, new StringBuilder());//下一层操作path.removeLast();//回溯}}}//判断是否为回文数public boolean check(StringBuilder str){char[] ch = str.toString().toCharArray();//stringBuilder转换为数组int left = 0, right = ch.length - 1;while(left < right){if(ch[left] != ch[right])return false;left++;right--;}return true;}
}


http://www.ppmy.cn/server/176786.html

相关文章

Docker --shm-size参数含义

文章目录 解释 解释 --shm-size 是一个用于 Docker 的参数&#xff0c;用来设置容器的共享内存大小。 在 Docker 中&#xff0c;/dev/shm 是一个临时文件系统&#xff08;tmpfs&#xff09;&#xff0c;用于存储共享内存。默认情况下&#xff0c;Docker 容器的共享内存大小是…

NO.51十六届蓝桥杯备战|堆算法题|第k小|除2|最小函数值|序列合并|舞蹈课(C++)

P3378 【模板】堆 - 洛谷 #include <bits/stdc.h> using namespace std;const int N 1e6 10; int n; int heap[N];void up(int child) {int parent child / 2;while (parent > 1 && heap[child] < heap[parent]){swap(heap[child], heap[parent]);chil…

「实战指南 」Swift 并发中的任务取消机制

网罗开发 &#xff08;小红书、快手、视频号同名&#xff09; 大家好&#xff0c;我是 展菲&#xff0c;目前在上市企业从事人工智能项目研发管理工作&#xff0c;平时热衷于分享各种编程领域的软硬技能知识以及前沿技术&#xff0c;包括iOS、前端、Harmony OS、Java、Python等…

vue中根据html动态渲染内容

需求&#xff1a;根据数据中的html&#xff0c;因为我是在做填空&#xff0c;所以是需要将html中的_____替换成input&#xff0c;由于具体需求我使用的是元素contenteditable代替的可编辑的input html部分 <div class"wrap"><component :is"rendered…

Springboot项目搭建(9)-分页与文件上传

1.添加依赖 在pom.xml中添加分页依赖 <dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.6</version> </dependency> 2.Entity层 定义实体类&a…

JDK 24新特性解读

ava 24 带来的新特性还是蛮多的&#xff0c;一共 24 个。Java 23 和 Java 23 都只有 12 个&#xff0c;Java 24的新特性相当于这两次的总和了。因此&#xff0c;这个版本还是非常有必要了解一下的。 下图是从 JDK8 到 JDK 24 每个版本的更新带来的新特性数量和更新时间&#x…

Windows 图形显示驱动开发-WDDM 3.0功能- 硬件翻转队列(六)

将 Present 请求映射到硬件翻转队列时间戳 在特定适配器上启用硬件翻转队列时&#xff0c;时间戳会附带所有翻转调用。 换句话说&#xff0c;KMD 不需要处理旧和新的 DxgkDdiSetVidPnSourceAddress 语义的组合。 OS 会自动将基于间隔的现有 Present API 请求转换为对 KMD 的基…

如何实现一个call函数?

在 JavaScript 中&#xff0c;call 是一个非常有用的方法&#xff0c;它允许你调用一个函数&#xff0c;并显式地指定该函数内部的 this 值。我们将手动实现一个类似 call 的函数&#xff0c;以深入理解其工作原理。 实现步骤 1. 定义 myCall 方法 首先&#xff0c;我们需要…