leetcode39--组合总数I

devtools/2024/9/25 9:23:34/

1. 题意

给一堆数,和一个目标和,求所有可能的不重复的组合。

组合总数

2. 题解

主要是去重,每次推入数到目标数组,将下次枚举的数的开头设为当前的下标就不会重复噜噜o_O

即如何避免 2 3 2 2\ 3\ 2 2 3 2 3 2 2 3\ 2\ 2 3 2 2这样的重复,这里的做法是,给定下次枚举的起始索引,

每次推入数的时候更新一下。

class Solution {
public:void gen(const vector<int> &candidates, vector<vector<int>> &ans,vector<int> &now,int csum,int target, int start){if ( csum == target ) {ans.emplace_back(now);return ;}int sz = candidates.size();for ( int i = start; i < sz; ++i ) {int v = candidates[i];if ( csum + v <= target) {now.emplace_back(v);gen( candidates, ans, now, csum + v, target, i);now.pop_back();}else {break;}}}vector<vector<int>> combinationSum(vector<int>& candidates, int target) {sort( candidates.begin(), candidates.end() );vector<vector<int>>  ans;vector<int> now;gen(candidates, ans, now, 0, target, 0);    return ans;}
};

http://www.ppmy.cn/devtools/5679.html

相关文章

Promise

Promise Promise 是异步编程的一种解决方案, 可以解决传统 Ajax 回调函数嵌套问题。 案例&#xff1a;模拟使用ajax连续发起两次请求 第一次请求monster.json获取到monster的id {"id": 1,"name": "黑山老妖" }第二次请求需要根据第一次获取到的…

20240417,友元 FRIEND

本来要学习的吃瓜吃了一下午 目录 3.1 全局函数做友元 3.2 友元类 3.3 成员函数做友元 三&#xff0c;友元 3.1 全局函数做友元 #include<iostream> using namespace std; class Building {friend void goodGay(Building* building);//好朋友&#xff0c;可以访问…

数据结构-基于ArrayList的源码模拟

文章目录 继承关系 :1. 构造方法的模拟2. 扩容机制的分析3. 查找方法的模拟4. 获取,修改元素的方法模拟5. 添加元素的模拟6. 删除元素的模拟7. removeAll与retainAll的模拟总结: 边缘方法以及总代码 继承关系 : 1. 构造方法的模拟 源码中我们的ArrayList的构造方法给出了三种实…

SQLite 的命令行 Shell(三十一)

返回&#xff1a;SQLite—系列文章目录 上一篇&#xff1a;SQLite FTS5 扩展&#xff08;三十&#xff09; 下一篇&#xff1a;SQLite—系列文章目录 1. 入门 SQLite 项目提供了一个名为 sqlite3&#xff08;或 Windows 上的sqlite3.exe&#xff09;的简单命令行程序 …

深度学习基础——卷积神经网络的感受野、参数量、计算量

深度学习基础——卷积神经网络的感受野、参数量、计算量 深度学习在图像处理领域取得了巨大的成功&#xff0c;其中卷积神经网络&#xff08;Convolutional Neural Networks&#xff0c;CNN&#xff09;是一种非常重要的网络结构。本文将介绍卷积神经网络的三个重要指标&#…

MongoDB快速启动

两种方法: 方式 1 &#xff1a;命令行参数方式启动服务 在 bin 目录中打开命令行提示符&#xff0c;输入如下命令&#xff1a; (mongod --dbpath..\data\db) mongod --dbpath..\data\db 方式 2 &#xff1a;配置文件方式启动服务 在解压目录中新建 config 文件夹&#xff0…

踏上R语言之旅:解锁数据世界的神秘密码(二)

R语言学习 文章目录 R语言学习1.数据的R语言表示2.多元数据的R语言调用3.多元数据的简单R语言分析 总结 1.数据的R语言表示 数据框&#xff08;data frame) R语言中用函数data.frame()生成数据框&#xff0c;其句法是&#xff1a; data.frame(data1,data2,…)&#xff0c;例如…

spring高级篇(二)

1、Aware和InitializingBean Aware和InitializingBean都与Bean的生命周期管理相关。 Aware接口: 概念: Aware接口是Spring框架中的一个标记接口&#xff0c;它表示一个类能够感知到&#xff08;aware of&#xff09;Spring容器的存在及其特定的环境。Spring框架提供了多个Awar…