当前位置:网站首页>LeetCode 40. 组合总和 II
LeetCode 40. 组合总和 II
2022-07-02 06:07:00 【大白羊_Aries】
题目描述
解法
其实这道题可以看成一个组合问题——计算 c a n d i d a t e s candidates candidates 中所有和为 t a r g e t target target 的子集,组合和子集其实是类似的,在看下面代码之前不放先理解一下 LeetCode 90. 子集 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();
}
}
};
边栏推荐
- CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
- 495.提莫攻击
- Software testing Q & A
- Common websites for Postgraduates in data mining
- Addchild() and addattribute() functions in PHP
- 如何使用MITMPROXy
- Software testing - concept
- Community theory | kotlin flow's principle and design philosophy
- Picture clipping plug-in cropper js
- Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
猜你喜欢

Deep learning classification network -- vggnet

Jetpack Compose 与 Material You 常见问题解答

Community theory | kotlin flow's principle and design philosophy

让每一位开发者皆可使用机器学习技术

Summary of MySQL constraints

Deep learning classification network -- Network in network

Introduce uview into uni app

Google play academy team PK competition, official start!

Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)

Ros2 --- lifecycle node summary
随机推荐
PHP extensions
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
Frequently asked questions about jetpack compose and material you
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
STC8H8K系列汇编和C51实战——数码管显示ADC、按键串口回复按键号与ADC数值
Redis key value database [advanced]
Shenji Bailian 3.52-prim
Database learning summary 5
Cookie plugin and localforce offline storage plugin
Web page user step-by-step operation guide plug-in driver js
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
Use some common functions of hbuilderx
Ti millimeter wave radar learning (I)
ES6的详细注解
ZABBIX server trap command injection vulnerability (cve-2017-2824)
页面打印插件print.js
Go learning notes integration