当前位置:网站首页>LeetCode 39. Combined sum
LeetCode 39. Combined sum
2022-07-02 06:10:00 【Great white sheep_ Aries】
Title Description
solution
This problem is super simple , If you've seen it LeetCode 77. Combine The solution of this article , You must know backtrace when i i i from s t a r t start start Start , So the next level of backtracking tree is from s t a r t + 1 start + 1 start+1 Start , To ensure that n u m s [ s t a r t ] nums[start] nums[start] This element will not be reused
// Standard framework of backtracking algorithm
for (int i = start; i < nums.size(); i++) {
// ...
// Recursively traverse the next level of backtracking tree , Pay attention to the parameters
backtrack(nums, i + 1, target);
// ...
}
however , Now we hope to reuse n u m s [ s t a r t ] nums[start] nums[start] This element , Then it's easy to do , I just want to i + 1 i + 1 i+1 Change to i i i that will do
// Standard framework of backtracking algorithm
for (int i = start; i < nums.size(); i++) {
// ...
// Recursively traverse the next level of backtracking tree
backtrack(nums, i, target);
// ...
}
Here is the complete implementation
class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
int trackSum;
vector<int> track;
backtrace(candidates, target, 0, track, trackSum);
return res;
}
void backtrace(vector<int>& candidates, int target, int start, vector<int>& track, int trackSum)
{
if (trackSum == target)
{
res.push_back(track);
return;
}
if (trackSum > target) return;
for (int i = start; i < candidates.size(); i++)
{
trackSum += candidates[i];
track.push_back(candidates[i]);
backtrace(candidates, target, i, track, trackSum);
track.pop_back();
trackSum -= candidates[i];
}
}
};
边栏推荐
- STC8H8K系列匯編和C51實戰——數碼管顯示ADC、按鍵串口回複按鍵號與ADC數值
- Scheme and implementation of automatic renewal of token expiration
- 经典文献阅读之--Deformable DETR
- Summary of MySQL constraints
- Lucene Basics
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
- 网络相关知识(硬件工程师)
- LeetCode 90. 子集 II
- 【C语言】筛选法求素数
- LeetCode 27. 移除元素
猜你喜欢
Classic literature reading -- deformable Detr
Eco express micro engine system has supported one click deployment to cloud hosting
经典文献阅读之--SuMa++
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
memcached安装
神机百炼3.54-染色法判定二分图
51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)
Zhuanzhuanben - LAN construction - Notes
随机推荐
ES6的详细注解
从设计交付到开发,轻松畅快高效率!
Shenji Bailian 3.54-dichotomy of dyeing judgment
使用HBuilderX的一些常用功能
From design delivery to development, easy and efficient!
[C language] screening method for prime numbers
如何使用MITMPROXy
Database learning summary 5
Regular expression summary
ZABBIX server trap command injection vulnerability (cve-2017-2824)
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
Introduce uview into uni app
STC8H8K系列汇编和C51实战——数码管显示ADC、按键串口回复按键号与ADC数值
Ti millimeter wave radar learning (I)
神机百炼3.52-Prim
Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
sudo提权
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
Mock simulate the background return data with mockjs
经典文献阅读之--Deformable DETR