当前位置:网站首页>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();
}
}
};
边栏推荐
- WLAN相关知识点总结
- LeetCode 83. 删除排序链表中的重复元素
- STC8H8K系列汇编和C51实战——数码管显示ADC、按键串口回复按键号与ADC数值
- Redis key value database [primary]
- Eco express micro engine system has supported one click deployment to cloud hosting
- Current situation analysis of Devops and noops
- 亚马逊aws数据湖工作之坑1
- LeetCode 77. 组合
- 500. Keyboard line
- Keepalived installation, use and quick start
猜你喜欢
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
从设计交付到开发,轻松畅快高效率!
Google Play Academy 组队 PK 赛,正式开赛!
Lucene Basics
官方零基础入门 Jetpack Compose 的中文课程来啦!
网络相关知识(硬件工程师)
MySQL transaction and isolation level
Google play academy team PK competition, official start!
Eco express micro engine system has supported one click deployment to cloud hosting
数据回放伴侣Rviz+plotjuggler
随机推荐
mock-用mockjs模拟后台返回数据
LeetCode 39. 组合总和
从设计交付到开发,轻松畅快高效率!
Redis Key-Value数据库【初级】
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
sudo提权
I/o impressions from readers | prize collection winners list
图片裁剪插件cropper.js
memcached安装
How vite is compatible with lower version browsers
步骤详解 | 助您轻松提交 Google Play 数据安全表单
Shenji Bailian 3.54-dichotomy of dyeing judgment
Current situation analysis of Devops and noops
Cookie plugin and localforce offline storage plugin
Ros2 --- lifecycle node summary
Verifying downloaded files using sha256 files
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
Detailed notes of ES6
Google play academy team PK competition, official start!
网络相关知识(硬件工程师)