当前位置:网站首页>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();
}
}
};
边栏推荐
- External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
- STC8H8K系列汇编和C51实战——数码管显示ADC、按键串口回复按键号与ADC数值
- [C language] screening method for prime numbers
- Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
- Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
- Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
- STC8H8K系列匯編和C51實戰——數碼管顯示ADC、按鍵串口回複按鍵號與ADC數值
- 【C语言】简单实现扫雷游戏
- WLAN相关知识点总结
- 社区说|Kotlin Flow 的原理与设计哲学
猜你喜欢

复杂 json数据 js前台解析 详细步骤《案例:一》

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

Keepalived installation, use and quick start

How to use mitmproxy

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

Spark overview

脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四

Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)

来自读者们的 I/O 观后感|有奖征集获奖名单
随机推荐
[C language] simple implementation of mine sweeping game
Cookie plugin and localforce offline storage plugin
来自读者们的 I/O 观后感|有奖征集获奖名单
Frequently asked questions about jetpack compose and material you
STC8H8K系列汇编和C51实战——串口发送菜单界面选择不同功能
LeetCode 39. 组合总和
Page printing plug-in print js
如何使用MITMPROXy
BGP中的状态机
LeetCode 78. 子集
token过期自动续费方案和实现
Redis Key-Value数据库 【秒杀】
Jetpack Compose 与 Material You 常见问题解答
Mock simulate the background return data with mockjs
The real definition of open source software
LeetCode 40. 组合总和 II
图片裁剪插件cropper.js
[C language] screening method for prime numbers
ZABBIX server trap command injection vulnerability (cve-2017-2824)
Ros2 --- lifecycle node summary