39. 组合总和
题目
给你一个 无重复元素 的整数数组 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 <= 302 <= candidates[i] <= 40candidates的所有元素 互不相同1 <= target <= 40
题目解析
代码详解:
-
全局变量:
path:存储当前正在生成的组合,使用LinkedList方便增删元素。result:存储所有符合条件的组合结果,是一个二维列表。
-
combinationSum方法:
- 功能:入口方法,调用
backtrace开始回溯,生成组合。 - 返回值:返回所有可能的组合,即
result。
- 功能:入口方法,调用
-
backtrace方法:-
功能:递归生成所有可能的组合。
-
参数说明:
candidates:输入数组。target:目标和。startindex:当前选择数字的起始索引,确保组合不会重复。sum:当前路径的数字和,用于判断是否达到目标值。
-
回溯逻辑:
- 递归终止条件:当sum大于目标和就返回,sum等于目标和就将路径记录到result中然后返回。
- 递归核心:
- 遍历数组中下标从
startindex开始 的数字。 - 将第
i个元素加入路径path。 - 递归调用
backtrace,将下一个数字的起始点设为i,并将路径sum加上当前节点元素。 - 回溯:移除当前路径中的最后一个数字,恢复状态。
- 遍历数组中下标从
-
关键细节
-
允许重复使用数字:
- 在回溯逻辑中,递归调用时的
startindex参数没有递增(传递当前索引i),表示可以重复选取当前数字。
- 在回溯逻辑中,递归调用时的
-
剪枝优化:
- 如果当前路径和
sum超过目标值target,直接返回,避免不必要的递归。
- 如果当前路径和
-
存储结果:
- 使用
new ArrayList<>(path)保存路径的副本,确保不受后续修改影响。
- 使用
java代码:
class Solution {
List <Integer>path=new LinkedList<>();//记录路径
List<List<Integer>> result=new ArrayList<>();//记录结果集
public void backtrace(int[] candidates,int target,int startindex,int sum){
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]);
backtrace(candidates,target,i,sum+candidates[i]);
path.removeLast();
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
backtrace(candidates,target,0,0);
return result;
}
}
剪枝优化
剪枝条件:在循环内,提前排除不可能的分支,减少递归深度,优化搜索效率。
实现方式:
- 排序:先对
candidates进行排序,使得数组中的元素递增。 - 剪枝判断:在回溯中,如果当前路径和加上候选数字大于目标值,则可以停止后续的迭代。

剪枝优化后代码:
class Solution {
List <Integer>path=new LinkedList<>();//记录路径
List<List<Integer>> result=new ArrayList<>();//记录结果集
public void backtrace(int[] candidates,int target,int startindex,int sum){
//if(sum>target)return;
if(sum==target){
result.add(new ArrayList<>(path));
return;
}
for(int i=startindex;i<candidates.length;i++){//回溯逻辑
if (sum + candidates[i] > target) break;
path.add(candidates[i]);
backtrace(candidates,target,i,sum+candidates[i]);
path.removeLast();
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates); // 先进行排序
backtrace(candidates,target,0,0);
return result;
}
}
注意!要剪枝就要先对数组进行排序,否则会剪掉正确的子树枝!!!
40. 组合总和 II
题目
给定一个候选人编号的集合 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 <= 1001 <= candidates[i] <= 501 <= target <= 30
题目解析

主要破题点在于,同一个树层上不可以选取相同的值,同一个树枝上可以选取,所以要引入一个标记数组标记是否是同一层或者是同一个树枝。
代码详细解释
主要变量
path:记录当前的组合路径。result:记录所有满足条件的组合结果。used:布尔数组,记录数组candidates中每个数字是否被使用,辅助去重。
主要逻辑
- 排序 (
Arrays.sort)- 将候选数组排序,方便通过判断相邻元素是否相等进行剪枝。
- 剪枝
- 如果
sum + candidates[i] > target,后续的循环无效,直接终止当前递归分支。 - 如果
candidates[i] == candidates[i-1],且前一个元素没有被使用 (used[i-1] == false),说明是同层重复的情况,直接跳过。
- 如果
- 递归结构
- 从
startindex开始,避免重复选择同一个元素。 - 对当前元素
candidates[i]:- 判断是否超出目标值,剪枝。
- 加入路径
path。 - 标记该元素为已使用
used[i] = true。 - 递归进入下一层,
startindex从i + 1开始。 - 回溯:移除路径中的当前元素,并将其标记为未使用。
- 从
边界条件
- 当
sum == target时,将当前路径添加到结果中。 - 如果
sum > target,直接返回。
class Solution {
List <Integer>path=new LinkedList<>();//记录路径
List<List<Integer>> result=new ArrayList<>();//记录结果集
boolean[] used;
public void backtrace(int[] candidates,int target,int startindex,int sum){
//if(sum>target)return;
if(sum==target){
result.add(new ArrayList<>(path));
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;
}
used[i] = true;//将当前节点标记为已用
path.add(candidates[i]);
backtrace(candidates,target,i+1,sum+candidates[i]);
used[i] = false;
path.removeLast();
}
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
used = new boolean[candidates.length];
// 加标志数组,用来辅助判断同层节点是否已经遍历
Arrays.fill(used, false);
Arrays.sort(candidates); // 先进行排序
backtrace(candidates,target,0,0);
return result;
}
}
131. 分割回文串
题目
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
示例 1:
输入:s = "aab" 输出:[["a","a","b"],["aa","b"]]
示例 2:
输入:s = "a" 输出:[["a"]]
提示:
1 <= s.length <= 16s仅由小写英文字母组成
题目解析
这段代码实现了一个解决字符串分割成回文子串的问题。具体来说,它使用回溯算法来找到所有可能的回文子串分割方式。代码的结构可以分为几个部分,下面是简短解释:
1. 核心方法:partition(String s)
- 目的:返回字符串
s的所有回文分割。 - 做法:将输入字符串
s赋值给全局变量str,然后调用backtrace(0, new StringBuilder())来开始回溯。
2. 回溯方法:backtrace(int startindex, StringBuilder sb)
- 目的:使用回溯算法从
startindex开始,尝试将字符串s分割成回文子串。 - 做法:
- 当
startindex达到字符串的末尾时,当前的分割路径(path)会被添加到结果result中。 - 对于每个可能的子串(从
startindex到i),检查是否是回文,如果是,则将其加入当前的路径path,并递归处理下一个子串。 - 回溯时,移除路径中的最后一个元素,继续尝试其他可能的分割。
- 当
Java代码如下:
class Solution {
List<String>path=new LinkedList<>();
List<List<String>>result=new ArrayList<>();
String str;
public void backtrace(int startindex,StringBuilder sb){
if(startindex>=str.length()){
result.add(new ArrayList<>(path));
return;
}
for(int i=startindex;i<str.length();i++){
sb.append(str.charAt(i));
if(check(sb)){
path.add(sb.toString());
backtrace(i+1,new StringBuilder());
path.removeLast();
}
}
}
public List<List<String>> partition(String s) {
str=s;
backtrace(0,new StringBuilder());
return result;
}
//helper method, 检查是否是回文
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;
}
}

1112

被折叠的 条评论
为什么被折叠?



