LeetCode 第40题:组合总和 II
1.LeetCode 第10题:正则表达式匹配2.LeetCode 第1题:两数之和3.LeetCode 第2题:两数相加4.LeetCode 第3题:无重复字符的最长子串5.LeetCode 第4题:寻找两个正序数组的中位数6.LeetCode 第8题:字符串转换整数 (atoi)7.LeetCode 第7题:整数反转8.LeetCode 第6题:Z字形变换9.LeetCode 第5题:最长回文子串10.LeetCode 第9题:回文数11.LeetCode 第11题:盛最多水的容器12.LeetCode 第12题:整数转罗马数字13.LeetCode 第13题:罗马数字转整数14.LeetCode 第14题:最长公共前缀15.LeetCode 第15题:三数之和16.LeetCode 第16题:最接近的三数之和17.LeetCode 第17题:电话号码的字母组合18.LeetCode 第18题:四数之和19.LeetCode 第19题:删除链表的倒数第N个结点20.LeetCode 第20题:有效的括号21.LeetCode 第21题:合并两个有序链表22.LeetCode 第22题:括号生成23.LeetCode 第23题:合并K个升序链表24.LeetCode 第24题:两两交换链表中的节点25.LeetCode 第25题:K 个一组翻转链表26.LeetCode 第26题:删除有序数组中的重复项27.LeetCode 第27题:移除元素28.LeetCode 第28题:找出字符串中第一个匹配项的下标29.LeetCode 第29题:两数相除30.LeetCode 第30题:串联所有单词的子串31.LeetCode 第31题:下一个排列32.LeetCode 第32题:最长有效括号33.LeetCode 第33题:搜索旋转排序数组34.LeetCode 第34题:在排序数组中查找元素的第一个和最后一个位置35.LeetCode 第35题:搜索插入位置36.LeetCode 第36题:有效的数独37.LeetCode 第37题:解数独38.LeetCode 第38题:外观数列39.LeetCode 第39题:组合总和
40.LeetCode 第40题:组合总和 II
41.LeetCode 第41题:缺失的第一个正数LeetCode 第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 <= 100
- 1 <= candidates[i] <= 50
- 1 <= target <= 30
解题思路
方法:回溯 + 剪枝
这是第39题的变体,主要区别在于:
- 每个数字只能使用一次
- 数组中可能包含重复数字
- 需要避免重复组合
关键点:
- 使用排序+剪枝避免重复组合
- 在同一层递归中跳过重复数字
- 每个数字只能使用一次
具体步骤:
- 对数组进行排序,方便剪枝和去重
- 使用回溯法搜索所有可能的组合:
- 记录当前已选数字和
- 同一层递归中跳过重复数字
- 每个数字只能使用一次
- 返回所有有效组合
时间复杂度:O(2^n)
空间复杂度:O(n),递归栈的深度
图解思路
回溯树分析表
层级 | 当前和 | 可选数字 | 选择 | 是否跳过 |
---|---|---|---|---|
0 | 0 | [1,1,2,5,6,7,10] | 1 | 否 |
1 | 1 | [1,2,5,6,7,10] | 1 | 否 |
2 | 2 | [2,5,6,7,10] | 6 | 否 |
回溯 | 1 | [2,5,6,7,10] | 2 | 否 |
回溯 | 1 | [5,6,7,10] | 5 | 否 |
剪枝策略示意图
当前数字 | 下一个数字 | 是否跳过 | 原因 |
---|---|---|---|
1 | 1 | 是 | 同层重复 |
2 | 2 | 是 | 同层重复 |
5 | 6 | 否 | 不同数字 |
6 | 7 | 否 | 不同数字 |
代码实现
public class Solution {
private IList<IList<int>> result = new List<IList<int>>();
private List<int> current = new List<int>();
public IList<IList<int>> CombinationSum2(int[] candidates, int target) {
Array.Sort(candidates);
Backtrack(candidates, target, 0);
return result;
}
private void Backtrack(int[] candidates, int remain, int start) {
if (remain == 0) {
result.Add(new List<int>(current));
return;
}
for (int i = start; i < candidates.Length; i++) {
// 剪枝:当前数字太大
if (candidates[i] > remain) break;
// 跳过同一层的重复数字
if (i > start && candidates[i] == candidates[i-1]) continue;
current.Add(candidates[i]);
Backtrack(candidates, remain - candidates[i], i + 1); // 注意这里是i+1
current.RemoveAt(current.Count - 1);
}
}
}
执行结果
- 执行用时:92 ms
- 内存消耗:42.5 MB
代码亮点
- 🎯 高效的重复组合处理
- 💡 巧妙的剪枝策略
- 🔍 优雅的回溯实现
- 🎨 代码结构清晰,易于理解
常见错误分析
- 🚫 没有正确处理重复数字
- 🚫 剪枝条件不完整
- 🚫 递归参数传递错误
- 🚫 结果集去重逻辑错误
解法对比
解法 | 时间复杂度 | 空间复杂度 | 优点 | 缺点 |
---|---|---|---|---|
暴力枚举 | O(2^n) | O(n) | 实现简单 | 效率低 |
回溯+剪枝 | O(2^n) | O(n) | 效率高 | 实现复杂 |
动态规划 | O(n*target) | O(target) | 性能稳定 | 不适用于此题 |
相关题目
- LeetCode 39. 组合总和 - 中等
- LeetCode 90. 子集 II - 中等
- LeetCode 216. 组合总和 III - 中等
- LeetCode 377. 组合总和 Ⅳ - 中等
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了