当前位置:网站首页>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];
}
}
};
边栏推荐
- LeetCode 90. 子集 II
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
- Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
- Common websites for Postgraduates in data mining
- 神机百炼3.53-Kruskal
- Sumo tutorial Hello World
- 图片裁剪插件cropper.js
- Generics and generic constraints of typescript
- Community theory | kotlin flow's principle and design philosophy
猜你喜欢

神机百炼3.54-染色法判定二分图

500. 键盘行

从设计交付到开发,轻松畅快高效率!

经典文献阅读之--Deformable DETR

Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles

Google Play Academy 组队 PK 赛,正式开赛!

深度学习分类网络--VGGNet

Current situation analysis of Devops and noops

MySQL transaction and isolation level

Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
随机推荐
Deep learning classification network -- alexnet
Ti millimeter wave radar learning (I)
Little bear sect manual query and ADC in-depth study
LeetCode 39. 组合总和
From design delivery to development, easy and efficient!
ROS create workspace
Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
页面打印插件print.js
Problems encountered in uni app development (continuous update)
BGP 路由優選規則和通告原則
uni-app开发中遇到的问题(持续更新)
Deep learning classification network -- vggnet
TI毫米波雷达学习(一)
How to use mitmproxy
Lucene Basics
How vite is compatible with lower version browsers
Generics and generic constraints of typescript
ROS2----LifecycleNode生命周期节点总结