当前位置:网站首页>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);
}
}
}
边栏推荐
- 移动端video兼容你需要知道的几点
- LeetCode每日一练 —— 27. 移除元素
- 【PHP】将 SESSION 数据保存到 Redis
- YOLO V2详解
- 计算机网络常见面试题目总结,含答案
- 金仓数据库 KingbaseES SQL 语言参考手册 (15. SQL语句:CREATE MATERIALIZED VIEW 到 CREATE SCHEMA)
- eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c
- Latte dht-phev products are very popular. Will the sales volume make Li Ruifeng figure it out?
- ipad下载的文件在哪里可以找到
- 一文看懂中国的金融体系
猜你喜欢

浅析接口测试

eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c
![Design of intelligent weighing system based on Huawei cloud IOT (STM32) [i]](/img/e4/4ebce448debf4bae308e2d5972a2a2.png)
Design of intelligent weighing system based on Huawei cloud IOT (STM32) [i]

银行业概览

Decompile jar files (idea environment)

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

2022/07/26 learning notes (day16) abstraction and interface

Talk about how to use redis to realize distributed locks?

Codeforces Round #810 (Div. 2)(A~C)

【PHP】将 SESSION 数据保存到 Redis
随机推荐
Uiobject2 of uiautomator2 common classes
These 22 drawing (visualization) methods are very important and worth collecting!
Digital transformation of enterprises has become a general trend, and it is important to choose the right online collaboration tools
LeetCode每日一练 —— 189. 轮转数组
MySQL 子查询使用方式
一家芯片公司倒在了B轮
Leetcode daily practice - 189. Rotation array
Codeforces Round #810 (Div. 2)(A~C)
上半年住户存款增加10.33万亿元,平均每天约571亿存款涌向银行
What should we do about the fragmentation of internal information? Try this
Scope in JS
金仓数据库 KingbaseES SQL 语言参考手册 (21. KES正则表达式支持)
金仓数据库 KingbaseES SQL 语言参考手册 (17. SQL语句: DISCARD 到 DROP LANGUAGE)
Solution to the third game of 2022 Niuke multi school league
2022/07/26 学习笔记 (day16) 抽象与接口
使用ECS和OSS搭建个人网盘
Collection of original IOS interview questions
Docker使用mysql:5.6和 owncloud 镜像,构建一个个人网盘,安装搭建私有仓库 Harbor
Intensive reading of the paper: yolov2 - yolo9000: better, faster, stronger
What is a knowledge management system? You need to know this