当前位置:网站首页>LeetCode_回溯_中等_40.组合总和 II
LeetCode_回溯_中等_40.组合总和 II
2022-07-26 19:08:00 【小城老街】
1.题目
给定一个候选人编号的集合 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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/combination-sum-ii
2.思路
(1)回溯
本题与LeetCode_回溯算法_中等_39.组合总和这题十分类似,只不过本题的每个数字在每个组合中只能使用一次,所以在进行回溯的过程中需要进行对应的剪枝操作。
3.代码实现(Java)
//思路1————回溯
class Solution {
//res用于保存最终结果
List<List<Integer>> res = new ArrayList<>();
//track用于记录回溯的路径
ArrayList<Integer> track = new ArrayList<>();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
if (candidates.length == 0) {
return res;
}
//先排序,以便让相等的元素排在一起,方便后续的剪枝操作
Arrays.sort(candidates);
backtrack(candidates, 0, target, 0);
return res;
}
public void backtrack(int[] candidates, int start, int target, int sum) {
if (sum == target) {
//找到目标组合,将其保存到 res 中
res.add(new ArrayList<>(track));
return;
}
if (sum > target) {
//组合元素之和超过target,直接结束
return;
}
//回溯算法框架
for (int i = start; i < candidates.length; i++) {
//剪枝操作,值相同的树枝,只遍历第一条
if (i > start && candidates[i - 1] == candidates[i]) {
continue;
}
//做选择
sum += candidates[i];
track.add(candidates[i]);
backtrack(candidates, i + 1, target, sum);
//撤销选择
sum -= candidates[i];
track.remove(track.size() - 1);
}
}
}
边栏推荐
- 花1200亿修一条“地铁”,连接4个万亿城市,广东在想啥?
- Leetcode daily practice - 26. Delete duplicates in an ordered array
- Linux 定时备份数据库并删除 N 天以前的数据
- 【OBS】Dropped Frames And General Connection Issues
- 客户案例|生学教育依托观测云打造可观测智慧教育新生态
- Is qiniu a channel for securities companies? Is it safe to open an account?
- ShardingSphere-JDBC 关键字问题
- 【二叉树】将二叉搜索树变平衡
- 金仓数据库 KingbaseES SQL 语言参考手册 (21. KES正则表达式支持)
- The authentication type 10 is not supported
猜你喜欢

ShardingSphere-JDBC 关键字问题

Intensive reading of the paper: yolov2 - yolo9000: better, faster, stronger

Excel-VBA 快速上手(十、提示框、可输入的弹出框)

【Android】Kotlin 快速编译背后的黑科技,了解一下~

N圆最密堆积、最小外接正方形的matlab求解(二维、三维等圆Packing 问题)

KVM virtualization

Zhongtian steel uses tdengine in GPS and AIS scheduling

plsql包
![[PHP] save session data to redis](/img/29/70a9f330b9f912ccbd57e865372439.png)
[PHP] save session data to redis

靠元宇宙和NFT,天下秀疯狂“割韭菜”?
随机推荐
Analysis of interface testing
【MySQL】 - 索引原理与使用
一年卖7亿,德州扒鸡赶考IPO
金仓数据库 KingbaseES SQL 语言参考手册 (14. SQL语句:COMMIT 到 CREATE LANGUAGE)
[PHP] MySQL native PHP operation - Tianlong eight steps
Live video source code to achieve the advertising effect of scrolling up and down
金仓数据库 KingbaseES SQL 语言参考手册 (16. SQL语句: CREATE SEQUENCE 到 DELETE)
N圆最密堆积、最小外接正方形的matlab求解(二维、三维等圆Packing 问题)
操作系统常见面试题目总结,含答案
plsql包
Canvas graphics
Detailed explanation of Yolo V2
金仓数据库 KingbaseES SQL 语言参考手册 (20. SQL语句: MERGE 到 VALUES)
[shell] Reprint: batch replacement find awk sed xargs
Zhongtian steel uses tdengine in GPS and AIS scheduling
u盘损坏怎么恢复原来数据,u盘损坏数据如何恢复
Linux regularly backs up the database and deletes the data n days ago
千亿酸奶赛道,乳企巨头和新品牌打响拉锯战
[internship experience] exception handling and URL result response data processing
金仓数据库 KingbaseES SQL 语言参考手册 (19. SQL语句: DROP TABLE 到 LOAD)