当前位置:网站首页>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();
}
}
};
边栏推荐
- PHP read file (read the specified line containing a string in the file)
- Some experience of exercise and fitness
- PHP parent
- Keepalived installation, use and quick start
- ESP8266与STC8H8K单片机联动——天气时钟
- 神机百炼3.53-Kruskal
- The official zero foundation introduction jetpack compose Chinese course is coming!
- 神机百炼3.54-染色法判定二分图
- servlet的web.xml配置详解(3.0)
- uni-app开发中遇到的问题(持续更新)
猜你喜欢
Google Play Academy 组队 PK 赛,正式开赛!
Summary of MySQL constraints
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
让每一位开发者皆可使用机器学习技术
如何使用MITMPROXy
Little bear sect manual query and ADC in-depth study
Spark overview
Comment utiliser mitmproxy
Step by step | help you easily submit Google play data security form
随机推荐
Common websites for Postgraduates in data mining
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
Fundamentals of software testing
Deep learning classification network -- vggnet
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
PHP 开发与测试 Webservice(SOAP)-Win
从设计交付到开发,轻松畅快高效率!
Ti millimeter wave radar learning (I)
STC8H8K系列汇编和C51实战——串口发送菜单界面选择不同功能
PHP obtains some values in the string according to the specified characters, and reorganizes the remaining strings into a new array
步骤详解 | 助您轻松提交 Google Play 数据安全表单
浏览器原理思维导图
Cookie plugin and localforce offline storage plugin
JWT工具类
格式校验js
来自读者们的 I/O 观后感|有奖征集获奖名单
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
Little bear sect manual query and ADC in-depth study
The official zero foundation introduction jetpack compose Chinese course is coming!
PHP development and testing WebService (soap) -win