当前位置:网站首页>Total number of combinations ii[each element can only be solved by + once]
Total number of combinations ii[each element can only be solved by + once]
2022-06-23 23:51:00 【REN_ Linsen】
Total number of combinations II
Preface
Seek arrangement and combination , Half are backtracking searches , Use techniques to reduce recursion stack depth , Use pruning to reduce running time . Total number of combinations II Compared with directly finding the combination sum as target, The difference is that each element can only be used once , And need to remove the weight ( Because the same element in the array is selected more than once ).
One 、 Total number of combinations II

Two 、 Backtracking search + duplicate removal
package everyday.medium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// Total number of combinations II
public class CombinationSum3 {
/* target: from candidates Select a number from the array , Let its sum be target, Each number can only be used once in a combination . It is also a standard backtracking , But the difference is that next time cur Add 1, instead of cur, Because each number can only be selected once . */
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> el = new ArrayList<>();
// General techniques for standard backtracking 1: Sort a sortable array , Let big numbers go first , And then with pruning , Reduce recursion stack depth .
Arrays.sort(candidates);
// Backtracking search to fill in matching combinations .
dfs(candidates, candidates.length - 1, 0, target, el, ans);
// Returns the filled combination .
return ans;
}
private void dfs(int[] candidates, int cur, int sum, int target, List<Integer> el, List<List<Integer>> ans) {
// When sum Greater than or equal to target when , End recursion , Recursive export 1.
if (sum >= target) {
if (sum == target) ans.add(new ArrayList<>(el));
return;
}
// Recursive export 2: Element used up .
// bug2: When there are the same elements , Will result in multiple repeated combinations .
int pre = 1 << 31;
for (int i = cur; i >= 0; i--) {
if (pre == candidates[i]) continue;
// Standard backtracking .
el.add(candidates[i]);
// bug1:cur Bits often write cur - 1, Should write i - 1, Select the variables for each round i.
dfs(candidates, i - 1, sum + candidates[i], target, el, ans);
el.remove(el.size() - 1);
// to update pre, Prevent the same elements from forming duplicate sets .
pre = candidates[i];
}
}
}
summary
1) Standard backtracking search exercise .
2) Backtracking techniques and pruning exercises .
3) How to go heavy , And some common problems in backtracking bug.
reference
边栏推荐
- Chrome plug-in features and case analysis of actual combat scenarios
- Why can't the netherworld fortress machine be remotely connected to the server? What are the ways to solve such problems?
- 19 MySQL optimizations commonly used in projects
- 2022山东健博会,济南国际大健康产业博览会,中国营养健康展
- Notepad++实用功能分享(正则行尾行首替换常用方法、文本比对功能等)
- Embedded interface review materials
- 三维向量场中的通量
- Simple understanding of responsive programming
- STM32-------定时器
- 嵌入式接口复习资料
猜你喜欢

高仿斗鱼 APP

再见,2020,这碗毒鸡汤,我先干了

图像分割-数据标注

2.摄像机标定

2018/GAN:Self-Attention Generative Adversarial Networks自我注意生成对抗网络

How to take the PMP Exam agile on June 25? Share your troubles

图扑软件智慧风电:数字孪生 3D 风机智能设备运维

产线工控安全有什么好的解决方案
![入参参数为Object,但传递过去却成了[object object] 是因为需要转为JSON格式](/img/8c/b1535e03900d71b075f73f80030064.png)
入参参数为Object,但传递过去却成了[object object] 是因为需要转为JSON格式

Visual explanation of clockwise inner curve in Green's formula hole digging method
随机推荐
Digital property management has become a trend. How can traditional property companies achieve digital butterfly change through transformation?
组合总数II[每个元素只能用一次 + 去重复解集]
Flux in three dimensional vector field
Postman返回值中文乱码????
What is the production process of enterprise website? How long does it take to design and build a website?
【 GBASE的那些事儿】系列直播活动第02期《GBase 8s高可用技术及案例分析法》
List<? Extensions T > and list <? Super T > difference
2022 point de connaissance de l'examen des ingénieurs en sécurité de l'information: contrôle d'accès
推荐4个Flutter重磅开源项目
AUTOCAD——总结CAD画圆角的三种方式
思考(八十七):协议加密与压缩
7、STM32——LCD
工作中一些常用的工具函数
Embedded interface review materials
PyQt5_QTableWidget分页单选右键菜单控件
《德阳市餐饮服务业油烟污染防治管理办法(征求意见稿)》之创新油烟监管
多门店药品进销存系统源码 大型连锁药店管理系统源码
Notepad++实用功能分享(正则行尾行首替换常用方法、文本比对功能等)
One person even broke up a Netease cloud music Cloud Village
matlab实现对图像批量重命名