当前位置:网站首页>39. 组合总和 && 40. 组合总和2 && 216. 组合总和3
39. 组合总和 && 40. 组合总和2 && 216. 组合总和3
2022-08-09 18:50:00 【糖葫芦零零七】
代码实现:
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
backtracking(candidates, target, 0, 0);
return res;
}
public void backtracking(int[] candidates, int target, int sum, int startIndex) {
if(sum == target){
// 找到目标集合
res.add(new ArrayList<>(path));
return;
}
for(int i = startIndex; i < candidates.length; i++){
if(sum + candidates[i] > target){
// 剪枝操作
break;
}
sum += candidates[i];
path.add(candidates[i]);
backtracking(candidates, target, sum, i); // i 不进行 +1 操作,表示还可以从自身进行累加
sum -= candidates[i]; // 回溯
path.remove(path.size() -1 ); // 回溯
}
}
}
代码实现:
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates); // 排序
backtracking(candidates, target, 0, 0);
return res;
}
private void backtracking(int[] candidates, int target, int sum, int startIndex) {
if(sum == target){
// 终止条件
res.add(new ArrayList<>(path));
return;
}
for(int i = startIndex; i < candidates.length; i++){
if(sum + candidates[i] > target){
break;
}
if(i > startIndex && candidates[i] == candidates[i - 1]){
// 去重部分,特别注意i > startIndex起始位置
continue;
}
sum += candidates[i];
path.add(candidates[i]);
backtracking(candidates, target, sum, i+1); // 这里的i要进行 +1 操作, 因为不能重复本身
sum -= candidates[i]; // 回溯
path.remove(path.size() - 1); // 回溯
}
}
}
代码实现:
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
int[] nums = {
1, 2, 3, 4, 5, 6, 7, 8, 9};
backtracking(nums, k, n, 0, 0);
return res;
}
private void backtracking(int[] nums, int k, int n, int sum, int startIndex) {
// 剪枝
if(sum > n){
return;
}
if(path.size() == k){
// 终止条件
if(sum == n){
res.add(new ArrayList<>(path));
return; // 如果path.size() == k 但sum != targetSum 直接返回
}
}
for(int i = startIndex; i < nums.length; i++){
if(sum + nums[i] > n){
break;
}
sum += nums[i];
path.add(nums[i]);
backtracking(nums, k , n , sum, i+1); // 由于不可以重复,所以i要进行 + 1 操作
sum -= nums[i]; // 回溯
path.remove(path.size() - 1); // 回溯
}
}
}
边栏推荐
- IDEA tools commonly used configuration
- [免费专栏] Android安全之安卓APK浅析
- Flume (六) --------- Flume 数据流监控
- 视频是主动学习吗?
- 《评估、创建和使用知识图谱的限制》2022最新230页博士论文,根特大学
- 智驾科技完成C1轮融资,此前2轮已融4.5亿元
- 基于Web的疫情隔离区订餐系统
- [免费专栏] Android安全之GDB动态调试APP
- [Free column] Xposed plug-in development for Android security [from scratch] tutorial
- golang单元测试:testing包的基本使用
猜你喜欢
看完这波 Android 面试题;助你斩获心中 offer
[Free column] Xposed plug-in development for Android security [from scratch] tutorial
IS31FL3737B 通用12×12 LED驱动器 I2C 42mA 40QFN
为什么数字钱包需要引入小程序生态
Paper sharing: "FED BN" uses the LOCAL BATCH NORMALIZATION method to solve the Non-iid problem
[免费专栏] Android安全之静态方式逆向APK应用浅析【手动注入smali+】+【IDA Pro静态分析so文件】+【IDA Pro基础使用讲解】
漏洞复现-redis未授权getshell
Fully automated machine learning modeling!The effect hangs the primary alchemist!
《痞子衡嵌入式半月刊》 第 60 期
[免费专栏] Android安全之动态代码注入技术(利用JDB调试APK)
随机推荐
Laravel之队列「建议收藏」
新出现的去中心化科学能够为科学领域带来什么?
Openharmony轻量系统实验--GPIO点灯
competed中访问ref为undefined
【kali-权限提升】(4.2.6)社会工程学工具包(中):中间人攻击工具Ettercap
Tims中国上市进入倒计时:年亏3.8亿 估值降至14亿美元
laravel 中配置文件.env解读
mysql 重复数据 分组 多条最新的记录
ebook下载 | 《 企业高管IT战略指南——企业为何要落地DevOps》
An overview of Office 365 Groups and how to create them
三星旗舰优惠千八,苹果优惠过千,国产旗舰只降五百打发叫花子
MFC tutorial
2021 RoboCom 世界机器人开发者大赛-本科组(决赛)
IDEA快捷代码实时模板
数学建模代码速成~赛前一个月~matlab~代码模板~吐血总结~三大模型代码(预测模型、优化模型、评价模型)
如何抑制告警风暴?
听音识情绪 | 程序员手把手教你搭建神经网络,更快get女朋友情绪,求生欲max!
pytest框架之mark标记功能详细介绍
uniapp离线推送华为厂商申请流程
重磅!上海985教授当选!全球仅4人!