当前位置:网站首页>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];
}
}
};
边栏推荐
- Lambda expressions and method references
- Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
- cookie插件和localForage离线储存插件
- Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
- Google play academy team PK competition, official start!
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
- Comment utiliser mitmproxy
- ROS2----LifecycleNode生命周期节点总结
- Go 学习笔记整合
- Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
猜你喜欢

uni-app开发中遇到的问题(持续更新)

Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability

Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish

Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken

BGP报文详细解释

社区说|Kotlin Flow 的原理与设计哲学

Zhuanzhuanben - LAN construction - Notes

51单片机——ADC讲解(A/D转换、D/A转换)

Spark overview

Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
随机推荐
Data playback partner rviz+plotjuggler
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
深度学习分类网络 -- AlexNet
Jetpack Compose 与 Material You 常见问题解答
步骤详解 | 助您轻松提交 Google Play 数据安全表单
492. Construction rectangle
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
从设计交付到开发,轻松畅快高效率!
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
Format check JS
Stc8h8k series assembly and C51 actual combat - keys allow key counting (using falling edge interrupt control)
uni-app开发中遇到的问题(持续更新)
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
Current situation analysis of Devops and noops
页面打印插件print.js
Spark overview
Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)