LeetCode39:组合总和

server/2024/10/21 7:36:05/

题目描述
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

在这里插入图片描述
代码

class Solution {
public:int sum = 0;vector<vector<int>>  res;vector<int> path;void backTracking(vector<int>& candidates,int target,int startIndex) {if (sum > target)return;if (sum == target) {res.push_back(path);return;}for (int i = startIndex; i < candidates.size(); i++) {sum += candidates[i];path.push_back(candidates[i]);backTracking(candidates, target,i);sum -= candidates[i];path.pop_back();}}vector<vector<int>> combinationSum(vector<int>& candidates, int target) {if (candidates.size() == 0) return res;backTracking(candidates, target,0);return res;}};

剪枝
上个代码对于sum已经大于target的情况,其实是依然进入了下一层递归,
只是下一层递归结束判断的时候,会判断sum > target的话就返回。

对总集合排序之后,如果下一层的sum(就是本层的 sum + candidates[i])已经大于target,就可以结束本轮for循环的遍历。

class Solution {
public:int sum = 0;vector<vector<int>>  res;vector<int> path;void backTracking(vector<int>& candidates, int target, int startIndex) {if (sum > target)return;if (sum == target) {res.push_back(path);return;}for (int i = startIndex; i < candidates.size() && sum+candidates[i]<=target; i++) {sum += candidates[i];path.push_back(candidates[i]);backTracking(candidates, target, i);sum -= candidates[i];path.pop_back();}}vector<vector<int>> combinationSum(vector<int>& candidates, int target) {if (candidates.size() == 0) return res;sort(candidates.begin(), candidates.end());backTracking(candidates, target, 0);return res;}
};

http://www.ppmy.cn/server/18136.html

相关文章

JavaScript-Vue入门

本文主要测分享Vue的一些基础 Vue简介 Vue.js 是一个构建数据驱动的 web 界面的渐进式框架。它的主要目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件。 下是一些 Vue 的主要特点和概念&#xff1a; 1. 响应式数据绑定&#xff1a;Vue 使用基于 HTML 的模板语法…

【MySQL】DML

1、DML简介 DML&#xff08;Data Manipulation Language、数据操作语言&#xff09;&#xff0c;用于添加、删除、更新和查询数据库记录&#xff0c;并检查数据完整性。 2. 添加数据 2.1 使用关键字 使用 INSERT 语句向表中插入数据。使用 VALUES语句添加 2.2 使用情况 2.2…

【C++】封装、继承和多态

引言 在现代软件开发中&#xff0c;面向对象编程&#xff08;Object Oriented Programming&#xff09;已经成为一种广泛应用的编程范式。C作为一种支持面向对象编程的语言&#xff0c;在封装、继承和多态方面提供了强大的特性。本文将介绍C中的封装、继承和多态概念&#xff…

Spark AQE 导致的 Driver OOM问题

背景 最近在做Spark 3.1 升级 Spark 3.5的过程中&#xff0c;遇到了一批SQL在运行的过程中 Driver OOM的情况&#xff0c;排查到是AQE开启导致的问题&#xff0c;再次分析记录一下&#xff0c;顺便了解一下Spark中指标的事件处理情况 结论 SQLAppStatusListener 类在内存中存…

科技论文网站:中国科技论文在线

文章目录 1. Intro2. Main3. Cons Evaluation彩蛋&#xff1a;科学素质 这是作者最后一次发 这种类型的宣传 科普文章 1. Intro 中国科技论文在线是经教育部批准&#xff0c;由教育部科技发展中心主办&#xff0c; 利用现代信息技术手段&#xff0c;打破传统出版物的概念&…

LeetCode 每日一题 ---- 【2739.总行驶距离】

LeetCode 每日一题 ---- 【2739.总行驶距离】 2739.总行驶距离解题方法&#xff1a;模拟 2739.总行驶距离 解题方法&#xff1a;模拟 根据题意进行模拟&#xff0c;主邮箱每减少 5 升油&#xff0c;汽车就可以行驶 10 公里&#xff0c;同时副油箱需要向主油箱注入 1 升油&…

ThingsBoard处理设备上报的属性并转换为可读属性

一、前言 二、案例 1、AI生成JSON数据体 2、将json数据体直接通过遥测topic发送查看效果 3、可查看目前整个数据都在一起 ​编辑 4、配置附规则链路 5、对msg的消息值&#xff0c;进行数据的转换&#xff0c;并从新进行赋值。 6、规则链路关联关系 7、再次通过MQTT发送遥…

Linux线程池

目录 1.模型样貌 2.代码模拟 1.模型样貌 2.代码模拟 pthread_pool.hpp 里面有很有多余的打印&#xff0c;就是因为59行初始化和60行是一样的&#xff0c;没有初始化_cond,所以就一直拍错&#xff0c;展示就保存下来吧&#xff0c;提醒自己&#xff01; Task.hpp main.cc 运行…