当前位置:网站首页>Leetcode40 - total number of combinations II
Leetcode40 - total number of combinations II
2022-04-23 02:01:00 【Xicheng Fengyu building】
LeetCode40- Total number of combinations II
One 、 Title Description
Two 、 Backtracking tree
Combination is a typical problem of finding a subset of a sequence , The limitation in the title is that the sequence found cannot be repeated , So this is also very simple , Just sort the original sequence in ascending order , When the backtracking tree expands horizontally (for loop ), Skip the repeated elements directly ( No recursion , See code for details )
3、 ... and 、 Total number of combinations II Code implementation
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
// You have to sort here first , Because it's going to be heavy
Arrays.sort(candidates);
solve(candidates, target, res, new ArrayList<>(), 0, 0);
return res;
}
public void solve(int[] candidates, int target, List<List<Integer>> res, List<Integer> curRes, int startIndex, int curSum) {
if (curSum == target) {
// It needs to be recorded
res.add(new ArrayList<>(curRes));
return;
}
if (curSum > target) {
return;
}
for (int i = startIndex; i < candidates.length; i++) {
if (curSum + candidates[i] > target) {
break;
}
// After the first , It is necessary to judge whether weight removal is necessary
if (i > startIndex) {
// Lateral weight removal
if (candidates[i] == candidates[i - 1]) {
continue;
}
}
curSum += candidates[i];
curRes.add(candidates[i]);
solve(candidates, target, res, curRes, i + 1, curSum);
curSum -= candidates[i];
curRes.remove(curRes.size() - 1);
}
}
}
版权声明
本文为[Xicheng Fengyu building]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220844276737.html
边栏推荐
- EBS:PO_EMPLOYEE_HIERARCHIES_ALL
- Redis memory recycling strategy
- W801 / w800 WiFi socket development (I) - UDP
- Sqlserver data transfer to MySQL
- W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
- ESP32蓝牙Bluetooth Controller API介绍
- Ziguang micro financial report is outstanding. What does the triple digit growth of net profit in 2021 depend on
- What code needs unit testing?
- 89 logistic回归用户画像用户响应度预测
- What is BGP server and what are its advantages?
猜你喜欢
随机推荐
npm——配置淘宝镜像
Quel est le fichier makefile?
2022 crane driver (limited to bridge crane) examination question bank and online simulation examination
代理IP可用率是不是等同于代理IP的效率?
如何设置电脑ip?
Is the sinking coffee industry a false prosperity or the eve of a broken situation?
中金财富跟中金公司是一家公司吗,安全吗
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
2022第六季完美童模 IPA国民赛领跑元宇宙赛道
PID精讲
[hands on learning] network depth v2.1 Sequence model
校园转转二手市场源码
ThinkPHP kernel development blind box mall source code v2 0 docking easy payment / Alibaba cloud SMS / qiniu cloud storage
不断下沉的咖啡业,是虚假的繁荣还是破局的前夜?
What is a dial-up server and what is its use?
MySQL active / standby configuration binary log problem
postman里面使用 xdebug 断点调试
C语言中如何“指名道姓”的进行初始化
How to choose a good dial-up server?
K zeros after leetcode factorial function