leetcode 39/40. Combination Sum/ II(组合数之和1,2)

Icarus Verilog和GTKwave使用简析 本简要介绍Icarus Verilog和GTKwave的使用,使用iverilog(Icarus Verilog)编译仿真verilog而使用GTKWave查看仿真数据波形下载安装 基本使用 iverilog使用 GTKWave使用 iverilog常用选项 使用库 注意 阅读详情

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

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

Note:

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

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]

给出一个数组和一个目标和target,让找出所有和为target的元素组合
可以使用重复元素

思路:
DFS和combination
DFS是排列,个数为N!combination为组合,个数为CNNC_{N}^NCNN​

DFS

 if cur.length == N
   result.add(cur);
  
 //每次遍历所有元素,找出所有排列,有重复 
 for every element i in candidates
    if (used[i]) continue;
    used[i] = true;
    cur.push(candidates[i])
    dfs(candidates, i, cur, result);
    cur.pop()
    used[i] = false;

combination:

 if cur.length == N
   result.add(cur);
   
  //start为传入参数,指定从哪个index开始遍历
 for  i = start to candidates.length
    cur.push(candidates[i])
    //注意这里从i+1开始,只挑后面的数,无重复
    combination(candidates, i+1, cur, result);
    cur.pop()

此题是combination sum,因此选combination的思路
但是因为可以重复选择同一元素,

//下次改为从i开始,可重复同一元素
    combination(candidates, i, cur, result);

同时为了降低时间复杂度,将原数组candidates排序,这样做有一个好处,当到某处的和已经大于target时,就不需要再遍历后面的元素

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(candidates == null || candidates.length == 0) {
            return result;
        }
        
        int n = candidates.length;
        Stack<Integer> cur = new Stack<>();
        Arrays.sort(candidates);
        
        combination(candidates, 0, target, result, cur);
        return result;
    }
    
    public void combination(int[] candidates, int start, int target, List<List<Integer>> result, Stack<Integer> cur) {
        if(target < 0) {
            return;
        }
        if(target == 0) {
        //这里一定要新建一个list,如果传参照的话后面元素会变empty
            result.add(new ArrayList<Integer>(cur));
            return;
        }
        
        for(int i = start; i < candidates.length; i++) {
            if(candidates[i] > target) {
                break;
            }
            cur.push(candidates[i]);
            //从i开始而不是i+1,因为可以使用重复数字
            combination(candidates, i, target - candidates[i], result, cur);
            cur.pop();
        }
    }

40题,仍然是组合,但是这次不能使用重复数字
那么就不需要改combination思路,直接用组合思路
combination:

 if cur.length == N
   result.add(cur);
   
  //start为传入参数,指定从哪个index开始遍历
 for  i = start to candidates.length
    cur.push(candidates[i])
    //注意这里从i+1开始,只挑后面的数,无重复
    combination(candidates, i+1, cur, result);
    cur.pop()

而且还有一个问题,虽然不使用相同index的数字,但是原数组candidates中是有重复数字的
这时可以用排序然后跳过重复元素的思路,但是这里注意比较的边界,因为我们每次是从i+1开始的下一次,所以下一次检查要从i+1往后开始检查重复,而不能把i+1这个边界之前的数字也拿来比较
比如[1,1,6]
第一次选1,然后下一次从第2个1开始,[1,6]
这时比较重复只在[1,6]这个小范围比较,而不能把上一step的1也拿来比较,那样的话就会跳过这一阶段的1,而导致解变少

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(candidates == null || candidates.length == 0) {
            return result;
        }
        
        Stack<Integer> cur = new Stack<>();
        Arrays.sort(candidates);
        
        combination(candidates, target, 0, result, cur);
        return result;
    }
    
    public void combination(int[] candidates, int target, int start, 
                        List<List<Integer>> result, Stack<Integer> cur) {
        if(target < 0) {
            return;
        }
        
        if(target == 0) {
            result.add(new ArrayList<Integer>(cur));
            return;
        }
        
        for(int i = start; i < candidates.length; i++) {
            if(candidates[i] > target) {
                break;
            }
            //注意比较start之后的部分,不能把边界之前的也拿来比较
            if(i > start && candidates[i] == candidates[i-1]) {
                continue;
            }
            cur.push(candidates[i]);
            combination(candidates, target - candidates[i], i+1, result, cur);
            cur.pop();
        }
        
    }
Win10 + Linux 双系统删除(Linux) 删除Linux所使用的磁盘分区 进入此电脑->计算机->管理->磁盘管理 阅读详情

相关推荐

PCB线路板的制作工艺流程分享!2021-08-21

Pcb板制作工艺流程:开料-钻孔-沉铜-压膜-曝光-显影-电铜-电锡-退膜-蚀刻-退锡-光学检测-印阻焊油-阻焊曝光、显影-字符-表面处理-成型-电测-终检-抽测-包装。具体如下:1、开料(CUT) 将原始的覆铜板切割成可以用在生产线上制作的板子过程2、钻孔 使PCB板层间产生通孔,以达到连通层间的目的。 3、沉铜 经过钻孔后的线路板在沉铜缸内会发生氧化还原反应,形成铜层对孔进行孔金属化,使原来绝缘的基材表面沉积上铜,达到层间电性相通。 4、压膜 压膜后的PCB板上面压了一层蓝色的干膜, 干膜是一个载体

jdbpcb88的博客 3265

Leetcode——组合总和1,2,3,4

1.题目 2. 题解 (1)暴力回溯 for循环表示树宽度,递归表示树深度 253,先取2,下轮仍可取253. 再取5,下轮可取53,再取3,下轮可取3 组合是无序的,先取了5,如仍可取2,会造成25和52重复 class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> path = new ArrayList<>(); p

Yawn 225

【开源~磁悬浮】零代码,纯硬件闭环:从霍尔到H桥的磁悬浮稳定术

本文详细介绍了如何不依赖任何代码,仅通过模拟和数字电路的组合,实现一个完整的磁悬浮闭环控制系统。从霍尔传感器检测位置,到运放电路进行信号调理与模拟PID控制,再到H桥驱动电磁铁执行纠偏,全程使用基础电子元器件搭建。文章深入剖析了系统原理、电路设计、调试技巧与实践经验,展现了纯硬件实现自动控制的独特魅力与挑战。

perl8的博客 636

LeetCode第39-40题 组合总和一和二

这两题非常相似,只有少量修改,所以放到一起 第一反应 递归,比如示例1,[2,3,6,7] target=7,我的想法是依次从target中减去一个待选数(比如7-2=5),然后对得到的数字再递归调用本体函数。 如果要优化的话,可以考虑对于得到的结果存进数组,以避免反复调用某个target浪费。 看看题解 当做一个树状图的搜索。每一个选择分支都有选择这个数字、不选这个数字两种,如果不选这个数字就把指针移动到下一个数字 看完题解再想 是不是,比如说树状图的搜索,如果在比如左侧的第一个分支表示选择2,在右侧的

m0_46132970的博客 416

双内核架构 Xenomai 4 安装教程

双内核 Xenomai 4 安装指南

^_^ 4493

固定效应模型-以stata为工具

固定效应模型(Fixed Effects Model)是一种面板数据分析方法,通过引入个体固定效应来控制个体间的异质性,并更准确地估计解释变量对因变量的影响。它在许多经济、社会科学和健康研究中被广泛应用。

侯小啾技术博客 2864

39.组合总和(Combination Sum)

39.组合总和(Combination Sum)题解回溯法+排序剪枝复杂度分析PythonJava(待完成) 题解 回溯法+排序剪枝 为了对算法进行剪枝处理,首先对candidatescandidatescandidates排序 特判,若candidatescandidatescandidates为空,则返回[][][] 回溯函数helper()helper()helper(),传入参数:...

朱师傅的博客 285

深度学习在地质勘探中的应用:使用Python构建卷积神经网络进行地震数据分析

在本文中,我们介绍了如何利用深度学习技术,特别是卷积神经网络(CNN),来处理和分析地震数据。我们使用了Python编程语言和TensorFlow框架构建了一个CNN模型,并使用地震数据进行了训练和评估。在本文中,我们将探讨如何利用深度学习技术,特别是卷积神经网络(CNN),来处理和分析地震数据。首先,我们需要获取地震数据集。通过本文的介绍,希望您能对深度学习在地质勘探领域的应用有更深入的了解,并能够应用这些技术来解决实际问题。在准备好地震数据和构建CNN模型后,我们需要将数据分为训练集和测试集。

斌擎科技 1483

36 数字组合(Combination Sum)

文章目录1 题目2 解决方案2.1 思路2.2 图解2.3 时间复杂度2.4 空间复杂度3 源码 1 题目 题目:数字组合(Combination Sum) 描述:给定一个候选数字的集合 candidates 和一个目标值 target。 找到 candidates 中所有的和为 target 的组合。在同一个组合中, candidates 中的某个数字出现次数不限。 所有数值 (包括 target ) 都是正整数. 返回的每一个组合内的数字必须是非降序的. 返回的所有组合之间可以是任意顺序. 解集不能包

SeeDoubleU的专栏 607

芒果YOLOv8改进52:主干Backbone篇之GhostNetV2:提出原创GhostNetV2 架构升级版:引入长距离注意力机制增强廉价操作,构建更强端侧轻量型骨干,打造高效轻量级检测器

长距离注意力机制增强廉价操作,打造轻量级检测器

包括YOLOv5、YOLOv7、YOLOv8等模型改进 1623

《2026国赛命题趋势前瞻:从近年真题看今年高教社杯的三大热点方向》

距离2026年“高教社杯”全国大学生数学建模竞赛,只剩下不到一个月的时间。如果你还在盲目刷往年真题,如果你还在死磕传统的微分方程和线性规划,如果你还在用五年前的论文模板——那么很遗憾,你很可能已经在起跑线上落后了。这不是危言耸听。过去三年,国赛的命题风格发生了肉眼可见的剧变。国赛正在从“数学技巧比拼”全面转向“数据科学能力+工程落地思维”的综合较量。2026年,恰逢国赛创办第35周年。在这个特殊的节点,命题会走向何方?哪些方向会大概率成为今年的“爆点”?

YOLO 29

【小白爬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的博客 221

ExpressLRS(ELRS)接收机技术全景:从历史演进、技术底层到产业生态全解析——上

ExpressLRS(ELRS)是一种由全球开源社区驱动的双向射频遥控链路协议栈,其接收机作为核心终端,承担信号接收解调、遥控指令解析、飞控数据交互与遥测回传等关键功能。自2018年问世以来,ELRS凭借极致性能、低成本、开放生态和强兼容性,从航模DIY方案发展为消费级无人机领域的事实标准,并逐步渗透工业级场景。 ELRS接收机采用模块化分层架构,支持2.4GHz和900MHz双频段,通过LoRa、FLRC、FSK多调制模式实现低延迟与远距离平衡。其核心技术包括自适应跳频抗干扰、TDD双向链路和极简数据包。

bother3000的博客 317

leetcode 39. Combination Sum和40. Combination Sum II

本题关键是去重。

weixin_41554427的博客 578

假人有限元模型

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

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

python解法:leetcode 39 Combination Sum 回溯加剪枝

下面详细介绍39. Combination Sum和40. Combination Sum II的python解法,主要用到了回溯加剪枝的思想。 leetcode 39 Given asetof candidate numbers (candidates)(without duplicates)and a target number (target), find all uni...

Sesen的木屋 802

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

40. Combination Sum II**

40. Combination Sum II** https://leetcode.com/problems/combination-sum-ii/ 题目描述 Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candi...

珍妮的选择的博客 334

leetcode-39/40 Combination Sum / II

问题描述: Given a set of candidate numbers(C) and a target number (T), find all uniquecombinations in C where the candidate numbers sums to T. The same repeatednumber may be chosen from C unlimited nu

woliuyunyicai的专栏 1195

LeetCode 39/40/216 Combination Sum/Combination Sum II/Combination Sum III

题目链接:点击这里 class Solution { public: vector<vector<int>> ans; //记录最终结果 vector<int> tmp; //记录中间结果 void robot(int idx, int now, int target, vector<int>&...

脚踏实地,仰望星空 201

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 | 40. Combination Sum II

40. Combination Sum II Description 描述:https://leetcode.com/problems/combination-sum-ii/description/ 题意:从给定数组中找出所有可能的组合(一个数只能取一次,数组中有重复的数),使得组合的和等于目标值。 Solution: (Java) class Solution { List<Lis...

Karmen199x的博客 242

leetcode 40. Combination Sum II-回溯算法

原题链接:40. Combination Sum II 【思路-Java】递归实现 本题是 leetcode 39. Combination Sum-回溯算法|递归|非递归 的延伸,本题中给定的数组元素有重复。对于[1,1,2],如果还是采用原先的处理,结果集中肯定会有重复的元素出现,那么怎么处理呢?方法就是只对本层循环中重复元素出现进行调用,而对下一层递归中重复元素跳过递归调用。为了使重复元

hdtopku 1602

【LeetCode】40. Combination Sum II 解题报告(Python)

题目分析: 这个题题目是让找不重复列表中可以组成目标值的所有组合,其中每个列表元素都只在当前组合中使用一次。他与【LeetCode】39. Combination Sum是非常相似的。不同是列表中的元素只能用一次,我们只需要想办法加上这个限定就可以了。代码中已有明确注释,不在累述。 测试代码: class Solution: def combinationSum2(self, candid...

L141210113的专栏 530

【C++】【LeetCode】39. Combination Sum&40. Combination Sum II

题目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

zjajgyy的博客 563

【LeetCode】解题40:Combination Sum II

LeetCode解题 40:Combination Sum II Problem 40: Combination Sum II [Medium]解题思路Solution (Java)修改过程 Problem 40: Combination Sum II [Medium] Given a collection of candidate numbers (candidates) and a targe...

Fayedily的博客 253

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+40+216+377 Combination Sum 相关】【python】

39Combination Sum是说让我们在给定数组C中找到所有的组合,使得组合中数字的和是target值。并且组合里的数字可以重复,也就是不限制C中每一个数字的使用。 这明显是回溯啦,大家记得让你判断有多少种呢就是动态规划,一旦让我们写出全部的组合那就是回溯。 回溯就是一直加进去数,满足条件就存下来,不满足呢就回退一步。代码如下。注意backtracking函数,利用for可以实现回溯。还要

u014251967的博客 1753

【一天一道LeetCode】#40. Combination Sum II

一天一道LeetCode系列(一)题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be use

ZeeCoder 5226
上一篇: leetcode 35. Search Insert Position(找到插入点)
下一篇: leetcode 41. First Missing Positive(第一个丢失的正数)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址