Leetcode 39. Combination Sum (递归)

Leetcode 39. Combination Sum --数组元素组合,和为目标值,一个元素可用无数次,输出每种组合,每种组合唯一 Given asetof candidate numbers (candidates)(without duplicates)and a target number (target), find all unique combinations incandidateswhere the candidate numbers sums totarget. Thesamerepeat... 阅读详情

39. Combination Sum

 Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:

[
  [7],
  [2, 2, 3]
]

题目链接:https://leetcode.com/problems/combination-sum/description/
题目大意:给定一个数组和一个目标数字。求数组中和为目标数字的所有数字组合,数字可以重复相加。
解题思路:dfs递归,到最底层若满足条件就把数字组合加入答案集合。
代码如下:
class Solution {
public:
    vector<vector<int> > ans;
    vector<int> ca;
    int n;
    void dfs(int st,vector<int> a,int cur,int target){  
        if(cur == target){
            ans.push_back(a);  //a是每个可能的答案,如果最终满足条件,即a中所有数字和为target,则把a加入答案数组
            return;
        }
        if(st >= n)
            return;
        for(int i = st;i < n;i++){
            if(cur + ca[i] <= target){
                int num = 1;
                vector <int> b = a;
                while(cur + ca[i] * num <= target){   //数字可以重复,多次加入同一个数字
                    b.push_back(ca[i]);
                    dfs(i + 1,b,cur + ca[i] * num,target);  //i是起始位置 
                    num ++;
                }
            }
        }
    }
    vector<vector<int> > combinationSum(vector<int>& candidates, int target) {
        n = candidates.size();
        ca = candidates;
        vector<int> a;
        sort(ca.begin(),ca.end());   //排序
        unique(ca.begin(), ca.end());   //去重
        dfs(0,a,0,target);  
        return ans;
    }
};



stm32学习笔记-启动文件 1、配置栈 Stack_Size EQU 0x00000400 AREA STACK, NOINIT, READWRITE, ALIGN=3 Stack_Mem SPACE Stack_Size __initial_sp EQU:相当于C中的define,定义Stack_Size = 0x00000400。 AREA: 指... 阅读详情

相关推荐

手把手教你绕过Incapsula reese84防护:从JS反混淆到动态Token生成全流程

本文深入解析了以Incapsula reese84为代表的现代Web客户端挑战防护机制,详细介绍了从JS反混淆、算法逆向到动态Token生成的全流程实战方法。文章系统阐述了如何通过模拟浏览器指纹、移植关键算法或嵌入JS引擎来构建自动化应对策略,并提供了稳健的令牌管理与系统架构设计思路,帮助开发者在合规前提下提升自动化工具的适应性。

jenkins8butler的博客 516

leetCode 39.Combination Sum(组合总和) 解题思路和方法

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from

Android进阶 5043

麒麟V10系统下微信PC版安装全攻略:从源配置到一键安装(附常见问题解决)

本文详细介绍了在麒麟V10系统下安装和优化微信PC版的完整流程,包括系统环境准备、软件源配置、多途径安装方案以及性能优化技巧。特别针对国产操作系统用户,提供了从基础安装到高级配置的一站式解决方案,并附有常见问题排查方法,帮助用户快速实现微信PC版的高效运行。

weixin_30500105的博客 712

leetcode 39. Combination Sum-回溯算法|递归|非递归

原题链接:39. Combination Sum 拓展博文:Combination Sum II | Java最短代码实现 【思路】 基本思路是先排好序,然后每次递归中把剩下的元素一一加到结果集合中,并且把目标减去加入的元素,然后把剩下元素(包括当前加入的元素)放到下一层递归中解决子问题。算法复杂度因为是NP问题,所以自然是指数量级的: public List> combina

hdtopku 4543

华为无线WiFi实战:基于802.1x与NPS的企业级认证部署指南

本文提供了一份详尽的华为无线WiFi企业级认证部署实战指南。通过整合802.1x协议与Windows NPS服务器,构建基于AD域账号的安全认证体系,彻底解决传统WiFi密码共享带来的安全风险。文章详细拆解了从NPS服务器配置、证书部署到华为AC端RADIUS模板、AAA方案、认证域及VAP模板调用的全流程,并附有终端连接测试与常见故障排查方法,助力企业实现高效、安全的无线网络接入管理。

weixin_29239661的博客 267

基于Simulink/Matlab的VSG并网系统单相接地与相间短路故障仿真建模与特性分析

本文详细介绍了基于Simulink/Matlab搭建虚拟同步发电机(VSG)并网系统仿真模型的方法,并重点分析了单相接地与相间短路故障下的系统特性。通过设置三相故障模块参数,模拟了故障发生时的电压、电流变化及VSG动态响应,为理解故障机理、优化控制策略及设计继电保护提供了仿真依据与实践指导。

t8u9v0w1x的博客 514

Leetcode 39:组合总和(最详细的解法!!!)

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的数字可以无限制重复被选取。 说明: 所有数字(包括 target)都是正整数。 解集不能包含重复的组合。 示例 1: 输入: candidates = [2,3,6,7], target = 7, 所求...

coordinate的博客 1万+

UE5在Ubuntu22.04.4下的高效开发环境配置指南

本文详细介绍了在Ubuntu 22.04.4系统上配置UE5高效开发环境的完整指南,包括系统准备、显卡驱动安装、UE5引擎编译与配置、开发工具链设置以及性能优化技巧。通过逐步指导,帮助开发者快速搭建稳定的UE5开发环境,解决常见问题,提升开发效率。

weixin_29251131的博客 382

leetcode39. 组合总和

题目: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的数字可以无限制重复被选取。 思路: 回溯+递归; 为了提高效率,可以先对数组排序,然后当遇到sum>target时就直接跳出循环,因为后面的肯定都不可以了; 代码中sum和candidates[i]不能提前相...

一个小白的日常 1105

基于 Qt 的轻量化工业组态软件设计与实现

本文设计并实现了一种基于Qt框架的轻量化工业组态软件,解决了传统组态软件体积庞大、部署复杂等问题。该软件采用模块化架构,集成可视化图形编辑、设备数据采集、脚本扩展等核心功能,具有跨平台运行、资源占用低、启动速度快等特点。通过关键技术优化,实现了高效稳定的数据采集、脚本控制和网络通信,可广泛应用于工业机器人HMI、小型PLC监控等轻量化场景。测试结果表明,该软件体积约30MB,运行内存占用60-80MB,启动时间1.2秒,满足工业轻量化监控需求。未来可扩展更多工业组件和协议支持,进一步提升应用价值。

谢谢大家的关注和点赞!这里只有纯纯的知识干货,没有一句废话。希望能实实在在帮到大家~要是觉得有用,别忘了给我点支持哟,你 140

微信公众号网页H5跳转小程序实战指南(开放标签wx-open-launch-weapp)微信H5跳转小程序、wx-open-launch-weapp、小程序跳转按钮、微信开放标签、H5打开小程序

跳转小程序(wx-open-launch-weapp)唤起App显示支付按钮等等……而我们这次要用的,就是跳转小程序的标签。场景是否支持服务号网页✅ 支持小程序静态网页托管✅ 支持订阅号❌ 不支持微信外部浏览器❌ 不支持这个“开放标签”功能其实很强大,除了跳转小程序,还有唤起APP、跳支付、调用客服等能力。但可惜资料零散,很多坑等着你亲自踩一遍。这篇文章我整理的流程基本涵盖了从“能不能跳”到“怎么跳”的完整路径,希望你能像我一样省下几个小时的踩坑时间。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 1163

<LeetCode>39. 组合总和

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。说明:所有数字(包括 target)都是正整数。解集不能包含重复的组合。 示例 1:输入: candidates = [2,3,6,7], target = 7, 所求解集为: [ [7], ...

Sonihr的犀牛园 3768

XDMA流传输模式

XDMA 流传输模式(Streaming DMA)​ 是 Xilinx FPGA 通过 PCIe 与主机进行 ​实时流式数据传输 的核心机制,

浩瀚之水的专栏 3433

LeetCode 39. Combination Sum 题解

本题是回溯算法的经典问题,主要考察对回溯思想的理解和应用。通过维护一个路径数组和起始索引,我们可以生成所有可能的组合,并确保不重复。回溯算法的核心思想是:通过选择、递归、回溯的过程,遍历所有可能的解空间,找到符合条件的解。可以重复选择同一元素,因此递归调用时起始索引仍为当前索引。为了避免重复组合,我们从起始索引开始遍历,而不是从数组的开头。当当前和大于目标值时,停止继续选择,进行剪枝,提高效率。这种方法不仅适用于组合总和问题,还可以应用于许多其他组合问题,例如子集问题、排列问题等。

cannonjinx的博客 4388

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

39.leetcode Combination Sum(medium)[递归回溯]

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited numb

qz530308783的专栏 243

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【小白爬Leetcode39】数组总和 Combination Sum

【小白爬Leetcode39】数组总和 Combination Sum题目方法一 暴力搜索(反例,不完全递归)方法二 回溯方法三 剪枝 题目 Leetcode39 medium\color{#ffaa00}{medium}medium 点击进入原题链接:数组总和 Combination Sum Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find al

weixin_41604040的博客 219

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

LeetCode 39. Combination Sum and LeetCode 40. Combination Sum II

LeetCode 39. Combination Sum Description: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums t...

My Blog 235

[leetcode javascript解题]Combination Sum

leetcode第39题Combination Sum描述如下: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The sam

zzxboy1的博客 784

40. Combination Sum II

题目 Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. Each number in candidates m...

努力学习的姜饼 110

LeetCode 39.数组总和 Combination Sum

题目链接 中文 https://leetcode-cn.com/problems/combination-sum/description/ 英文 https://leetcode.com/problems/combination-sum/description/ 解题思路 1.首先对candidates进行排序,这样可以在循环遍历时先取到小的加数,保证加数有效性,并且可以在加数较大时直接退出循环,...

qq_32147775的博客 368

leetcode 39. Combination Sum DFS深度优先搜索 + 按照index做递归

Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from...

JackZhangNJU的专栏 701

LeetCode 39. Combination Sum 题解(回溯 / DFS)

给定一个由互不相同的正整数构成的数组 candidates,以及一个目标值 target,找出所有可以使数字和为 target 的组合。数组中的数字可以被重复选择任意次,但组合之间不能重复,返回结果的顺序可以任意。

六六哥的博客 370

LeetCode刷题:39. Combination Sum

LeetCode刷题:39. Combination Sum 原题链接:https://leetcode.com/problems/combination-sum/description/ Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique comb...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 286

Leetcode 39 Combination Sum

Leetcode 39 Combination Sumclass Solution { public: vector<vector<int>> sum; vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<int> result; sort(can

自己的学习笔记 209

LeetCode(40) Combination Sum II

在LeetCode(39) Combination Sum代码基础上增加了一些代码。具体分析参见LeetCode(39) Combination Sumclass Solution { public: void dfsSearch(vector<int> &candidates, int serialNumber, vector<int> &combinationIndex, int dept

一只不断成长的藏獒 317

39. Combination Sum

/* * 39. Combination Sum * 2016-5-3 by Mingyang * The same repeated number may be chosen from C unlimited number of times. * 注意在实现中for循环中第一步有一个判断,那个是为了去除重复元...

weixin_30401605的博客 85
上一篇: Leetcode 15. 3Sum
下一篇: Leetcode 46. Permutations (全排列)
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值