【LeetCode】解题40:Combination Sum II

奇安信 2025 年护网蓝队初选笔试题(附答案解析) 熬夜为大家整理了 奇安信 2025 年护网蓝队初选笔试题,(关注我我会持续更新)涵盖 SQL 注入、Web 安全、渗透测试、二进制安全 等核心知识点,并附上详细答案解析,助力大家高效备考!12.下列关于虚表指针描述正确的是(1.5分)✅ B. 虚表指针存储的是虚表的首地址❌ A. 任何类对象都存在虚表指针(仅含虚函数的类有)❌ C. 虚表指针位于对象内存空间的起始处(由编译器决定)❌ D. 虚表指针是否存在不影响对象所占内存空间的大小(存在时会增加指针大小)📌 解析:虚表指针(vptr)用于实现多态,指向 阅读详情

Problem 40: Combination Sum II [Medium]

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 may only be used once in the combination.

Note:

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

Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

Example 2:

Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]

来源:LeetCode

解题思路

与39题类似。
基本思路为回溯算法,采用深度优先,并使用剪枝减少无用搜索。

  • 首先将候选数组按升序排序。
  • 每一轮循环从候选数组中选择一个数x加入组合,比较target'与x的大小,此处target'代表每一轮中新的目标值,根据比较结果分为三种情况:
    a. 如果target' - x = 0,说明组合中sum = target,将该种组合加入最终结果result列表里,并剪枝。
    b. 如果target' - x < 0,说明组合中sum > target,不能再加入任何数,也不能将x替换为更大的候选数,因此这里直接剪枝。
    c. 如果target' - x > 0,说明组合中sum < target,还可以继续深入搜索,更新target' = target' - x,进行递归搜索。
  • 注意1:题目中说每个数只能用一次,因此每次深度搜索时要从下一个坐标(index + 1)开始搜索。
  • 注意2:为了防止重复组合,在backtracking()中的循环内,即广度搜索时,如果下一个数在candidates数组中是重复的,则需要跳过搜索。

要点:回溯算法剪枝

Solution (Java)

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Arrays.sort(candidates);
        int N = candidates.length;
        if(N == 0) return result;
        List<Integer> comb = new ArrayList<Integer>();
        backtracking(candidates, 0, N, target, comb, result);
        return result;
    }
    private void backtracking(int [] candidates, int index, int N, int target, List<Integer> comb, List<List<Integer>> result){
        int temp = 0;
        for(int i = index; i < N; i++){
        	// skip duplicate candidates
            while(i < N && candidates[i] == temp) i++;
            if(i == N) return;
            temp = candidates[i];
            int next_target = target - candidates[i];
            if(next_target == 0){
                comb.add(candidates[i]);
                result.add(comb);
                return;
            }
            else if(next_target < 0){
                return;
            }
            else{
                List<Integer> next_comb = new ArrayList<Integer>(comb);
                next_comb.add(candidates[i]);
                // search i+1 ~ N-1, candidates[i] may only be used once
                backtracking(candidates, i+1, N, next_target, next_comb, result);
            }
        }
    }
}

修改过程

  • 没有在广度搜索时跳过candidates中重复的数字,导致输入[10,1,2,7,6,1,5]8,输出的集合为:[[1,1,6],[1,2,5],[1,7],[1,2,5],[1,7],[2,6]],其中[1,2,5]和[1,7]都输出了两遍,是因为candidates中有两个1,没有跳过。
SimNow模拟环境实战:用vnpy_ctp连接期货市场的5个关键配置步骤 本文提供了一份详细的量化交易实战指南,重点讲解如何使用vnpy_ctp插件连接SimNow期货模拟环境的5个关键配置步骤。内容涵盖SimNow环境选择、vnpy_ctp核心参数解析、实战连接脚本编写、深度排错技巧以及向实盘迁移的准备,旨在帮助量化交易开发者快速搭建可靠的测试环境,避免常见陷阱。 阅读详情

相关推荐

【Latex】【附安装文件】Miktex和TexStudio的安装与配置

这代表“构建并运行”,一般来说,我们只需要点这个按钮来运行Latex代码。保存一下代码文件,再运行一次,PDF文件会出现在与代码文件相同的文件夹下。安装完成后,你可能会跳转到一个页面中,你看到这个页面就意味着安装成功了。其实不需要你敲,你只要将下面的代码复制粘贴到你刚刚的文件中即可。成功安装MikTex后,我们需要进行更新,减少后续使用的麻烦。如果懂得将安装路径设置在你想要的盘中,你可以自己设置。双击上文的.exe文件后,你会看到如下画面。如果不懂,为避免不必要的麻烦,请直接点击“

未来就在脚下。 1万+

LeetCode40. Combination Sum II 解题报告(Python)

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

L141210113的专栏 524

YOLOv8改进策略【卷积层】| TIP 2025 MFA 多阶段特征聚合 大核扩感受 + 多梯度融合,强效增强弱小目标特征

红外弱小目标尺寸极小、信噪比低,在特征传递中极易丢失信息;常规卷积感受野单一、梯度源不足,难以同时捕捉局部细节与全局上下文,导致小目标特征表达弱、检测漏检。因此提出多阶段特征聚合 MFA,用大核卷积+多分支+通道注意力强化小目标特征提取。

Limiiiing的博客 38

LeetCode40. Combination Sum II(Medium)

1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合 注意:(1)每个可能的答案中,数组中的每一个元素只能使用一次;(2)数组存在重复元素;(3)数组中都是正整数;(...

aysk1112的博客 146

【一天一道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 5225

[leetcode] 40. Combination Sum II 解题报告

题目链接:https://leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to

小榕流光的专栏 815

Leetcode1-100: 40. Combination Sum II

Leetcode1-100: 40. Combination Sum II问题描述解题思路代码实现 问题描述 **题目要求:**见上一题 解题思路 跟上一题的思路基本一样,只不过这题的条件变成每个元素只能出现一次了,只需要微调上一题的解法即可。 代码实现 public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>>

Y123iwantit的博客 288

LeetCode算法题之40. Combination Sum II(medium)

题目描述: 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 candidate...

DaMoWangZQ的博客 219

LeetCode40题思悟——组合总数IIcombination-sum-ii

LeetCode40题思悟——组合总数IIcombination-sum-ii) 文章目录LeetCode40题思悟——组合总数IIcombination-sum-ii)知识点预告题目要求示例我的思路优秀解法差异分析知识点小结 知识点预告 数组的排序处理; 分治思想的应用; 递归结果的返回处理; 题目要求 给定一个数组 candidates 和一个目标数 target ,找出 can...

博采众长,自成一派; 317

LeetCode 40. Combination Sum II (Java版; Medium)

welcome to my blog LeetCode 40. Combination Sum II (Java版; Medium) 题目描述 Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates&nb...

littlehaes的博客 399

Leetcode #40. Combination Sum II 组合求和2 解题报告

1 解题思想这道题是昨天的升级版,先看看这个 Leetcode #39. Combination Sum 组合求和 解题报告这道题的改变就是每个位置的数只能用一次了,但是如果本身就给了多个的话就无所谓。 基本方法一样,关键是对于重复的那里处理: 请看我代码里面的这一条 关键在于这部防止重复,规则就是排序后,如果当前位置i的数字和i-1的一样,那么必须要i用过后,i-1才能用,不然必须跳过

MebiuW的专栏 1166

LeetCode解题126:Word Ladder II(BFS算法

LeetCode解题 126:Word Ladder II(BFS算法)Problem 126: Word Ladder II [Hard]解题思路Solution (Java)修改过程 Problem 126: Word Ladder II [Hard] Given two words (beginWord and endWord), and a dictionary’s word list, ...

Fayedily的博客 771

LeetCode解题188:Best Time to Buy and Sell Stock IV(动态规划)

LeetCode解题 188:Best Time to Buy and Sell Stock IV (动态规划)Problem 188: Best Time to Buy and Sell Stock IV [Hard]解题思路Solution (Java)修改过程 Problem 188: Best Time to Buy and Sell Stock IV [Hard] Say you hav...

Fayedily的博客 594

LeetCode解题123:Best Time to Buy and Sell Stock III(动态规划)

LeetCode解题 123:Best Time to Buy and Sell Stock III (动态规划)Problem 123: Best Time to Buy and Sell Stock III [Hard]解题思路I. O(2n)时间+O(3n)空间II. O(2n)时间+O(n)空间III. O(n)时间+O(4)空间Solution (Java) Problem 123: B...

Fayedily的博客 487

LeetCode解题119:Pascal's Triangle II

LeetCode解题 118:Pascal's Triangle II Problem 118: Pascal's Triangle II [Easy]解题思路Solution (Java) Problem 118: Pascal’s Triangle II [Easy] Given a non-negative index k where k ≤ 33, return the kth index...

Fayedily的博客 444

LeetCode解题78:Subsets

LeetCode解题 78:Subsets Problem 78: Subsets [Medium]解题思路Solution (Java)修改过程 Problem 78: Subsets [Medium] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The so...

Fayedily的博客 443

LeetCode解题44:Wildcard Matching(动态规划解法)

LeetCode解题 44:Wildcard Matching(动态规划解法)Problem 44: Wildcard Matching [Hard]解题思路Solution (Java) Problem 44: Wildcard Matching [Hard] Given an input string (s) and a pattern (p), implement wildcard patt...

Fayedily的博客 429

LeetCode解题10:Regular Expression Matching(动态规划解法)

LeetCode解题 10:Regular Expression Matching(动态规划解法)Problem 10: Regular Expression Matching [Hard]解题思路Solution (Java) Problem 10: Regular Expression Matching [Hard] Given an input string (s) and a patter...

Fayedily的博客 411

LeetCode解题122:Best Time to Buy and Sell Stock II

LeetCode解题 122:Best Time to Buy and Sell Stock II Problem 122: Best Time to Buy and Sell Stock II [Easy]解题思路Solution (Java) Problem 122: Best Time to Buy and Sell Stock II [Easy] Say you have an array...

Fayedily的博客 366

LeetCode解题32:Longest Valid Parentheses(多解法:栈+动态规划+计数器)

LeetCode解题 32:Longest Valid Parentheses(多解法:栈+动态规划+计数器)Problem 32: Longest Valid Parentheses [Hard]解题思路1. 栈2. 动态规划3. 计数器Solution (Java) Problem 32: Longest Valid Parentheses [Hard] Given a string cont...

Fayedily的博客 352

LeetCode解题31:Next Permutation

LeetCode解题 31:Next Permutation Problem 31: Next Permutation [Medium]解题思路Solution (Java)修改过程 Problem 31: Next Permutation [Medium] Implement next permutation, which rearranges numbers into the lexicogr...

Fayedily的博客 352

LeetCode解题714:Best Time to Buy and Sell Stock with Transaction Fee(动态规划)

LeetCode解题 714:Best Time to Buy and Sell Stock with Transaction Fee (动态规划)Problem 714: Best Time to Buy and Sell Stock with Transaction Fee [Medium]解题思路Solution (Java) Problem 714: Best Time to Buy an...

Fayedily的博客 339

IEC 60749-26-2018第 26 部分:静电放电( ESD )敏感度测试 人体模型( HBM ).rar

半导体器件 机械和气候试验方法 (英文)———第 1 部分:总则;———第 2 部分:低气压;———第 3 部分:外部目检;———第 4 部分:强加速稳态湿热试验( HAST );———第 5 部分:稳态温湿度偏置寿命试验———第 6 部分: 高温贮存———第 7 部分:内部水汽含量测试和其它残余气体分析———第 8 部分:密封———第 9 部分:标志面耐久性———第 10 部分:机械冲击———第 11 部分:快速温度变化 双液槽法;———第 12 部分:扫频振动;———第 13 部分:盐雾;———第 14 部分:引出端强度(引线牢固性);———第 15 部分:通孔安装器件的耐焊接热;———第 16 部分:粒子碰撞噪声检测(PINT)———第 17 部分:中子辐照;———第 18 部分:电离辐射(总剂量);———第 19 部分:芯片剪切强度;———第 20 部分:塑封表面安装器件耐潮湿和焊接热综合影响;———第 20-1 部分:对潮湿和焊接热综合影响敏感的表面安装器件的操作、包装、标志和运输;———第 21 部分:可焊性;———第 22 部分:键合强度;———第 23 部分:高温工作寿命;———第 24 部分:加速耐湿 无偏置强加速应力试验(HSAT)———第 25 部分:温度循环———第 26 部分:静电放电( ESD )敏感度测试 人体模型( HBM );———第 27 部分:静电放电( ESD )敏感度测试 机器模型( MM );———第 28 部分:静电放电(ESD)敏感度试验 带电器件模型(CDM)器件级———第 29 部分:闩锁试验———第 30 部分:非密封表面安装器件在可靠性试验前的预处理;———第 31 部分:塑封器件的易燃性(内部引起的);———第 32 部分:塑封器件的易燃性(外部引起的);———第 33 部分:加速耐湿 无偏置高压蒸煮———第 34 部分:功率循环———第 35 部分:塑封电子元器件的声学扫描显微镜检查———第 36 部分:恒定加速度———第 37 部分:基于加速度计的板面液滴测试方法———第 38 部分:带存储器的半导体器件软误差测试方法———第 39 部分:半导体元件用有机材料中水分扩散率和水溶性的测量———第 40 部分:基于应变仪的板面液滴试验方法———第 41 部分:非易失性存储器的标准可靠性试验方法———第 42 部分:温湿度贮存。———第 43 部分:集成电路可靠性认证计划指南———第 44 部分:半导体器件的中子辐照单粒子效应(SEE)试验

本仓库提供DevExpress VCL v21.1.7 for Delphi 11的资源文件下载 DevExpress VCL

DevExpress VCL v21.1.7 for Delphi 11 资源下载资源描述本仓库提供DevExpress VCL v21.1.7 for Delphi 11的资源文件下载。DevExpress VCL Controls是Devexpress公司旗下最老牌的用户界面套包,所包含的控件有:数据录入、图表、数据分析、导航、布局等。该控件能帮助您创建优异的用户体验,提供高影响力的业务解决方案,并利用您现有的VCL技能为未来构建下一代应用程序。资源特点丰富的控件库:包含数据录入、图表、数据分析、导航、布局等多种控件,满足各种界面开发需求。优异的用户体验:通过精心设计的控件,帮助开发者创建出色的用户界面,提升用户体验。高影响力的业务解决方案:提供强大的功能和灵活的定制选项,帮助开发者快速构建高影响力的业务应用程序。兼容Delphi 11:专为Delphi 11开发,充分利用Delphi 11的最新特性,确保最佳的开发体验。使用说明下载资源:点击下载按钮获取DevExpress VCL v21.1.7 for Delphi 11的资源文件。安装与配置:按照官方文

上一篇: 【LeetCode】解题39:Combination Sum
下一篇: 【LeetCode】解题41:First Missing Positive
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值