当前位置:网站首页>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); // 回溯
}
}
}
边栏推荐
- AWS CodePipeLine 跨账号部署ECS
- 2022.08.08_每日一题
- 双屏协作更高效,华硕灵耀X 双屏Pro 2022创作体验再升级
- Flume (六) --------- Flume 数据流监控
- 优秀的 Verilog/FPGA开源项目介绍(三十一)- OFDM
- 力扣 899. 有序队列
- An overview of Office 365 Groups and how to create them
- Intensive reading of the paper: VIT - AN IMAGE IS WORTH 16X16 WORDS: TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE
- 2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-RC-u5 树与二分图
- 基于CC2530 E18-MS1-PCB Zigbee DIY作品(二)
猜你喜欢
2022深圳(软考高级)信息系统项目管理师认证报名
小满nestjs(第四章 前置知识装饰器-实现一个GET请求)
看完这波 Android 面试题;助你斩获心中 offer
shell之变量详解,让你秒懂!
《痞子衡嵌入式半月刊》 第 60 期
Open Source Summer | List Details Display Based on Ruoyi Architecture
ebook下载 | 《 企业高管IT战略指南——企业为何要落地DevOps》
【IoT毕设】STM32与机智云自助开发平台的宠物智能喂养系统
Intensive reading of the paper: VIT - AN IMAGE IS WORTH 16X16 WORDS: TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE
[免费专栏] Android安全之和平精英(FZ)APK逆向分析
随机推荐
[免费专栏] Android安全之动态代码注入技术(利用JDB调试APK)
C#/VB.NET: Extract text and pictures from PowerPoint document
一图详解沃土云创计划高校教师参与全流程
hdu 2094 产生冠军(STL map || 拓扑 || STL set)
mysql 重复数据 分组 多条最新的记录
优秀的 Verilog/FPGA开源项目介绍(三十一)- OFDM
视频是主动学习吗?
看完这波 Android 面试题;助你斩获心中 offer
三星旗舰优惠千八,苹果优惠过千,国产旗舰只降五百打发叫花子
2022.08.05_每日一题
[免费专栏] Android安全之Android奇淫run-as命令
小满nestjs(第六章 nestjs cli 常用命令)
Openharmony轻量系统实验--GPIO点灯
Intensive reading of the paper: VIT - AN IMAGE IS WORTH 16X16 WORDS: TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE
全自动化机器学习建模!效果吊打初级炼丹师!
shell之变量详解,让你秒懂!
源码编译安装与yum和rpm软件安装详解
小满nestjs(第五章 nestjs cli)
MYSQL物理存储文件的页和INNOBUF的页是否有大小区别?
DP-Differential Privacy概念介绍