当前位置:网站首页>LeetCode 40. Combined sum II
LeetCode 40. Combined sum II
2022-07-02 06:11:00 【Great white sheep_ Aries】
Title Description
40. Combinatorial summation II
solution
In fact, this problem can be regarded as a combinatorial problem —— Calculation c a n d i d a t e s candidates candidates All and for t a r g e t target target Subset , Combination and subset are similar , Before looking at the following code, please understand LeetCode 90. A subset of II
class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
if (candidates.size() == 0) return res;
int trackSum = 0;
vector<int> track;
sort(candidates.begin(), candidates.end());
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++)
{
if (i > start && candidates[i] == candidates[i - 1]) continue;
trackSum += candidates[i];
track.push_back(candidates[i]);
backtrace(candidates, target, i + 1, track, trackSum);
trackSum -= candidates[i];
track.pop_back();
}
}
};
边栏推荐
- Use some common functions of hbuilderx
- Classic literature reading -- deformable Detr
- 492.构造矩形
- 51单片机——ADC讲解(A/D转换、D/A转换)
- Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
- Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
- [C language] simple implementation of mine sweeping game
- ZABBIX server trap command injection vulnerability (cve-2017-2824)
- 495. Timo attack
- Ti millimeter wave radar learning (I)
猜你喜欢
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
Can't the dist packaged by vite be opened directly in the browser
复杂 json数据 js前台解析 详细步骤《案例:一》
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
From design delivery to development, easy and efficient!
How vite is compatible with lower version browsers
社区说|Kotlin Flow 的原理与设计哲学
让每一位开发者皆可使用机器学习技术
Deep learning classification network -- alexnet
从设计交付到开发,轻松畅快高效率!
随机推荐
servlet的web.xml配置详解(3.0)
Eco express micro engine system has supported one click deployment to cloud hosting
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
I/o impressions from readers | prize collection winners list
Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
页面打印插件print.js
cookie插件和localForage离线储存插件
Redis key value database [advanced]
Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
Redis Key-Value数据库 【高级】
Mock simulate the background return data with mockjs
Community theory | kotlin flow's principle and design philosophy
LeetCode 77. 组合
[C language] simple implementation of mine sweeping game
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
从设计交付到开发,轻松畅快高效率!
Let every developer use machine learning technology