当前位置:网站首页>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
边栏推荐
- World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
- Shardingsphere broadcast table and binding table
- Error in face detection and signature of Tencent cloud interface
- About how to import C4d animation into lumion
- leetcode:27. 移除元素【count remove小操作】
- How to install mysql-5.7.9 in RPM mode under Linux system
- 搭建网站是用物理机还是云主机好?
- Dimension C China helping farmers in rural areas warms people's hearts the third stop is jiabaoguo farm
- Performance introduction of the first new version of cdr2022
- What is BGP server and what are its advantages?
猜你喜欢

Introduction to esp32 Bluetooth controller API

Shardingsphere sub database and sub table

什么是bgp服务器,有哪些优势?

Longest common subsequence (record path version)

如何对代理IP进行分类?

2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track

BGP服务器在什么业务场景会被用到?

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio

教程】如何用GCC“零汇编”白嫖MDK

《维C中国》乡村助农暖人心第三站嘉宝果农场
随机推荐
教程】如何用GCC“零汇编”白嫖MDK
How to write the resume of Software Test Engineer so that HR can see it?
The leader / teacher asks to fill in the EXCEL form document. How to edit the word / Excel file on the mobile phone and fill in the Excel / word electronic document?
Analyze the advantages and disadvantages of tunnel proxy IP.
2022第六季完美童模 IPA國民賽領跑元宇宙賽道
mb_ substr()、mb_ Strpos() function (story)
2022.4.20-----leetcode. three hundred and eighty-eight
Is the sinking coffee industry a false prosperity or the eve of a broken situation?
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
ESP32使用freeRTOS的消息队列
A simple and open source navigation website source code
NPM yarn startup error [resolved]
世界读书日 | 技术人不要错过的好书(IT前沿技术)
Leetcode 112 Total path (2022.04.22)
Esp32 message queue using FreeRTOS
代理IP可用率是不是等同于代理IP的效率?
2022.4.22-----leetcode. three hundred and ninety-six
PHP & laravel & master several ways of generating token by API and some precautions (PIT)
Shardingsphere read write separation
单片机和4G模块通信总结(EC20)
