当前位置:网站首页>组合总数[标准回溯 + 回溯技巧--降低栈深度]
组合总数[标准回溯 + 回溯技巧--降低栈深度]
2022-06-22 20:33:00 【REN_林森】
前言
对于排列组合问题,一般用DFS搜索,状态规划来空间换时间,减少没必要的重复计算。回溯常见技巧,将数组排序,大数/小数先行,降低函数栈深度;回溯常见技巧剪枝,降低运行时间。
一、组合总数

二、标准回溯
1、搜索回溯
// 组合总和
public class CombinationSum {
/* target:给一些候选数字,然后无限用,取和为target的组合情况。 dfs不断寻找即可(无重复元素,无零元素),当和 大于等于 target时则递归结束 | 所用元素用完时递归结束。 */
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> el = new ArrayList<>();
// dfs寻找
dfs(candidates, 0,0, target, ans, el);
// ans已填充满
return ans;
}
private void dfs(int[] candidates, int cur,int sum, int target, List<List<Integer>> ans, List<Integer> el) {
if (sum >= target) {
if (sum == target)
ans.add(new ArrayList<>(el));
return;
}
// 标准回溯
for(int i = cur;i < candidates.length;i++){
el.add(candidates[i]);
dfs(candidates,i,sum + candidates[i],target,ans,el);
el.remove(el.size() - 1);
}
}
}
2、降低栈深度
// 让递归深度变浅一点,通过逆序来实现。
class CombinationSum2 {
/* target:给一些候选数字,然后无限用,取和为target的组合情况。 dfs不断寻找即可(无重复元素,无零元素),当和 大于等于 target时则递归结束 | 所用元素用完时递归结束。 */
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> el = new ArrayList<>();
// 先给candidates排个序,让大数先行,从而快速剪枝,降低函数栈深度。
Arrays.sort(candidates);
// dfs寻找
dfs(candidates, candidates.length - 1,0, target, ans, el);
// ans已填充满
return ans;
}
private void dfs(int[] candidates, int cur,int sum, int target, List<List<Integer>> ans, List<Integer> el) {
if (sum >= target) {
if (sum == target)
ans.add(new ArrayList<>(el));
return;
}
// 标准回溯
for(int i = cur;i >= 0;i--){
el.add(candidates[i]);
dfs(candidates,i,sum + candidates[i],target,ans,el);
el.remove(el.size() - 1);
}
}
}
总结
1)标准回溯,DFS与动规。
2)排序降低栈深度与剪枝。
参考文献
[1] LeetCode 组合总数
边栏推荐
- Crud+ form verification for spa project development
- 【ROS 入门学习 】CmakeList.txt 和Packages.xml释义
- 6-1 二叉搜索树的操作集
- Capital and share increase of avita technology under Chang'an is settled: Ningde times will hold about 24%!
- [GWCTF 2019]mypassword XSS
- ACM. The beauty of hj45 name ●●
- Kdd'22 | Ali: fine tuning CTR estimation based on EE exploration
- 二级造价工程师考前必备15个知识点来了!祝你旗开得胜!
- IDC publie le rapport sur la gouvernance des données en Chine
- TC397 Flash
猜你喜欢

RealNetworks vs. Microsoft: the battle in the early streaming media industry

Liunx installing MySQL

Implementation of depth traversal adjacency matrix of figure 6-5

CVPR2022 | 用于重采图像的特征解耦学习与动态融合

Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA
![[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields](/img/3b/8916c543c149292221ce500209df0d.png)
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields

Lesson 029: Documents: a task? After class test questions and answers

CyCa children's physique etiquette Shenzhen training results assessment successfully concluded

IDC releases China Data Governance Report Yixin Huachen No. 1

IDC發布中國數據治理報告 億信華辰第一
随机推荐
【论文解读】关于基于视觉无人机自主降落平台的论文梳理
The interception of Chinese and English strings in Oracle database is different
TC397 Flash
Query es page subscript exceeds 10000
CVPR2022 | 用于重采图像的特征解耦学习与动态融合
Lesson 028: Documents: because I know you, I will never forget the after-school test questions and answers [no title]
KDD'22 | 阿里: 基于EE探索的精排CTR预估
引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能
Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers
A hundred lines of code to realize reliable delay queue based on redis
Lesson 027: Set: in my world, you are the only after-school test question and answer
Research hotspot - Official publicity! The release time of JCR zoning and impact factors will be determined in 2022!
Quick sort template & considerations
ACM. The beauty of hj45 name ●●
7-9 super Mary
Lesson 021: functions: lambda expressions | after class test questions and answers
Implementation of breadth traversal adjacency matrix of 6-6 graph
【知乎知识主推荐】 无人机中的城堡- 专注于无人机在不同技术领域的应用
Based on AI driven macromolecular drug discovery, "Huashen Zhiyao" obtained nearly 500million yuan of round a financing
Lesson 025: Dictionary: after class test questions and answers when the index is not easy to use