当前位置:网站首页>4. < tag backtracking, combination and pruning > lt.39 Combined sum + lt.40 Combined sum II DBC
4. < tag backtracking, combination and pruning > lt.39 Combined sum + lt.40 Combined sum II DBC
2022-06-09 12:01:00 【Caicai's big data development path】
lt.39. Combinatorial summation
[ Case needs ]

[ Thought analysis ]


[ Code implementation one , Non pruning optimization method ]
class Solution {
List<List<Integer>> lists = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
if(candidates.length == 0 || candidates == null){
return lists;
}
backTracking(candidates, target, 0, 0);
return lists;
}
public void backTracking(int[] candidates, int target, int sum, int startIndex){
// Recursion end condition
if(sum > target)return;
if(sum == target){
lists.add(new ArrayList<>(path));
return;
}
// Single layer recursive logic
for(int i = startIndex; i < candidates.length; i++){
sum += candidates[i];
path.add(candidates[i]);
backTracking(candidates, target, sum, i);
sum -= candidates[i];
path.remove(path.size() - 1);
}
}
}
[ Code implementation one , Pruning optimization solution ]
lt.40. Combinatorial summation II
[ Case needs ]

[ Thought analysis ]

[ Code implementation one , Non pruning optimization method ]
class Solution {
List<List<Integer>> lists = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
boolean[] used = new boolean[candidates.length];
Arrays.sort(candidates);
backTracking(candidates, target, 0, 0, used);
return lists;
}
public void backTracking(int[] candidates, int target, int sum, int startIndex, boolean[] used){
if(sum > target)return;
if(sum == target){
lists.add(new ArrayList<>(path));
return;
}
// Single layer recursive logic
for(int i = startIndex; i < candidates.length; i++){
// duplicate removal
if(i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false)continue;
sum += candidates[i];
used[i] = true;
path.add(candidates[i]);
backTracking(candidates, target, sum, i + 1, used);
sum -= candidates[i];
used[i] = false;
path.remove(path.size() - 1);
}
}
}
[ Code implementation 2 , Pruning optimization solution ]
边栏推荐
- Learning notes of segmentation, paging, page table and quick table
- Win10 20h2 was officially released, and the list of old and new features was compared
- Windows远程时提示CredSSP加密数据库修正
- 系统集成项目管理工程师考试说明
- ConfigurationManager姿势快闪
- IPv6 address assignment
- 8.<tag-回溯和全排列>lt.46. 全排列 + lt.47. 全排列 II
- Journal of the Chinese Academy of Sciences Bao Yungang: to accelerate the development of key core technologies, we must grasp the laws of Technological Development
- How to make money through Hongmeng ecology?
- H3C认证云计算高级工程师
猜你喜欢

3. < tag backtracking, combination and pruning > lt.17 Letter combination of telephone number

7.<tag-回溯和子集问题>lt.70.子集 + lt.90.子集 II

【管理知多少】中文“其他”、英文“OTHER”、日文“その他”.......

LR11安装报错:此计算机上缺少vc2005_sp1_with_atl_fix_redist,请安装所有缺少的必要组件,然后重新运行此安装。
![[untitled]](/img/c1/35479e8bd3832e3b6d9452768c4f13.png)
[untitled]

Prompt credssp encryption database correction when windows is remote

13.<tag-二叉树和BST基础>lt.450. 删除二叉搜索树中的节点 dbc

5. bracket generation

Relay alphafold! Star pharmaceutical science and technology released a new era of tbind opening molecular protein complex structure prediction

Security evaluation of commercial password application
随机推荐
Use of component El scrollbar
Spark is too slow to write Doris solution
H3C certified cloud computing senior engineer
8. search insertion position
6.两两交换链表中的节点
马斯克 “取消交易”威胁奏效 推特同意开放数据库供其核查
虚拟机出现entering emergency mode,使用xfs_rapair出现Device or resource busy解决
09 | the fourth step: the construction and delivery of the middle office
Win11 officially released new features
H3C认证网络工程师
5.括号生成
SIGIR 2022 | CMI: micro video recommendation combining comparative learning and multi interest mining
从2022年的这几篇论文看推荐系统序列建模的趋势
Prompt credssp encryption database correction when windows is remote
爱可可AI前沿推介(6.9)
Preparation guide for 2022 soft test senior network planning designer
LR11安装报错:此计算机上缺少vc2005_sp1_with_atl_fix_redist,请安装所有缺少的必要组件,然后重新运行此安装。
tag贪心-刷题预备知识-贪心解题方法 + lt.455. 分发饼干 + lt.376. 摆动序列
[signalr complete series] Realization of signalr real-time communication in net core
Aiko ai Frontier promotion (6.9)