当前位置: 首页 > news >正文

北京和君网站建设项目平台

北京和君网站建设,项目平台,asp.net做的网站文字控件随窗口大小不变化,电子商务网原题地址:. - 力扣(LeetCode) 题目描述 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用 一次 。 注意&#xff…

原题地址:. - 力扣(LeetCode)

题目描述

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次 。

注意:解集不能包含重复的组合。 

示例 1:

输入: candidates =[10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]

提示:

  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30

解题思路

从给定的候选数字数组 candidates 中找出所有和为 target 的组合,但与之前的问题不同,这个变种允许使用候选数字数组中的数字多次,且组合中的数字不一定要连续。代码中使用了深度优先搜索(DFS)算法来解决这个问题,并进行了一些优化:

  1. 预处理:首先对候选数字数组进行排序,并使用 freq 数组来存储每个不同数字的频率。
  2. DFS:深度优先搜索函数 dfs 用于递归地构建所有可能的组合。

dfs 方法中,我们有两个选择:

  • 跳过当前数字:直接递归调用 dfs 方法,尝试下一个数字。
  • 选择当前数字:如果当前目标 rest 大于等于候选数字,则将该数字添加到 sequence 列表中,并递归调用 dfs 方法,目标减去该数字的倍数,直到超过剩余目标或超过当前数字的频率。

代码实现

class Solution {// 存储每个数字的频率List<int[]> freq = new ArrayList<>();// 存储所有有效的组合List<List<Integer>> ans = new ArrayList<>();// 存储当前的组合List<Integer> sequence = new ArrayList<>();public List<List<Integer>> combinationSum2(int[] candidates, int target) {// 对候选数字数组进行排序Arrays.sort(candidates);// 计算每个数字的频率for (int num : candidates) {int size = freq.size();// 如果当前数字与前一个数字不同,或者freq为空,则添加新的频率记录if (freq.isEmpty() || num != freq.get(size - 1)[0]) {freq.add(new int[]{num, 1});} else {// 否则,增加相同数字的频率++freq.get(size - 1)[1];}}// 开始深度优先搜索dfs(0, target);// 返回所有有效的组合return ans;}public void dfs(int pos, int rest) {// 如果剩余目标为0,说明找到了一个有效的组合,添加到结果列表中if (rest == 0) {ans.add(new ArrayList<>(sequence));return;}// 如果位置超出freq数组范围,或者剩余目标小于当前数字,返回if (pos == freq.size() || rest < freq.get(pos)[0]) {return;}// 先不选择当前数字,递归搜索下一个数字dfs(pos + 1, rest);// 选择当前数字,最多选择至多等于剩余目标或当前数字频率的最小值int most = Math.min(rest / freq.get(pos)[0], freq.get(pos)[1]);for (int i = 1; i <= most; ++i) {// 将当前数字添加到组合中sequence.add(freq.get(pos)[0]);// 递归搜索下一个数字,目标减去当前数字dfs(pos + 1, rest - i * freq.get(pos)[0]);}// 回溯,移除添加的当前数字for (int i = 1; i <= most; ++i) {sequence.remove(sequence.size() - 1);}}
}

复杂度分析

  • 时间复杂度:最坏情况下,DFS 会尝试所有可能的组合,但由于进行了剪枝(即跳过大于剩余目标的数字),实际的时间复杂度通常要好于 O(2^n),但最坏情况下仍然是指数级别的。

  • 空间复杂度:空间复杂度主要取决于递归栈的深度和 sequence 列表的大小。最坏情况下,递归栈的深度和 sequence 的大小都不超过 target,所以空间复杂度为 O(target)。


文章转载自:
http://paradoxist.qpnb.cn
http://rhinolaryngology.qpnb.cn
http://laughing.qpnb.cn
http://sobersides.qpnb.cn
http://germany.qpnb.cn
http://obtrude.qpnb.cn
http://deontic.qpnb.cn
http://dreadnaught.qpnb.cn
http://mat.qpnb.cn
http://ducky.qpnb.cn
http://daric.qpnb.cn
http://sf.qpnb.cn
http://amidohydrolase.qpnb.cn
http://unsatisfactory.qpnb.cn
http://spotty.qpnb.cn
http://phospholipin.qpnb.cn
http://cabob.qpnb.cn
http://telencephalon.qpnb.cn
http://celaeno.qpnb.cn
http://ostensory.qpnb.cn
http://merioneth.qpnb.cn
http://dniester.qpnb.cn
http://driography.qpnb.cn
http://recidivity.qpnb.cn
http://turkophil.qpnb.cn
http://gangsterdom.qpnb.cn
http://quantification.qpnb.cn
http://thermokinematics.qpnb.cn
http://robber.qpnb.cn
http://modicum.qpnb.cn
http://unprison.qpnb.cn
http://vitellogenesis.qpnb.cn
http://stupendous.qpnb.cn
http://taborin.qpnb.cn
http://typhous.qpnb.cn
http://overpoise.qpnb.cn
http://tablespoonful.qpnb.cn
http://ishtar.qpnb.cn
http://palazzo.qpnb.cn
http://porcelain.qpnb.cn
http://magellan.qpnb.cn
http://immalleable.qpnb.cn
http://weeklong.qpnb.cn
http://thessalonian.qpnb.cn
http://sasebo.qpnb.cn
http://nasally.qpnb.cn
http://oyer.qpnb.cn
http://colorimeter.qpnb.cn
http://antipatriotic.qpnb.cn
http://ladified.qpnb.cn
http://munitionment.qpnb.cn
http://hooker.qpnb.cn
http://centrical.qpnb.cn
http://lactometer.qpnb.cn
http://inconsonant.qpnb.cn
http://breezeless.qpnb.cn
http://bulgy.qpnb.cn
http://purserette.qpnb.cn
http://amygdala.qpnb.cn
http://artfully.qpnb.cn
http://brouhaha.qpnb.cn
http://semicomic.qpnb.cn
http://fleet.qpnb.cn
http://ptah.qpnb.cn
http://weasel.qpnb.cn
http://clavicembalo.qpnb.cn
http://vermicular.qpnb.cn
http://aberdonian.qpnb.cn
http://corallite.qpnb.cn
http://tunney.qpnb.cn
http://shipman.qpnb.cn
http://salt.qpnb.cn
http://ahg.qpnb.cn
http://debunk.qpnb.cn
http://piteous.qpnb.cn
http://appease.qpnb.cn
http://coalite.qpnb.cn
http://ceremoniously.qpnb.cn
http://gonadotropic.qpnb.cn
http://picksome.qpnb.cn
http://exempligratia.qpnb.cn
http://neoterist.qpnb.cn
http://listeriosis.qpnb.cn
http://frankforter.qpnb.cn
http://withdrawal.qpnb.cn
http://censure.qpnb.cn
http://witness.qpnb.cn
http://retrobulbar.qpnb.cn
http://tetany.qpnb.cn
http://safest.qpnb.cn
http://aviculture.qpnb.cn
http://slider.qpnb.cn
http://fluctuating.qpnb.cn
http://dicing.qpnb.cn
http://elven.qpnb.cn
http://yellowish.qpnb.cn
http://rusa.qpnb.cn
http://perceptibly.qpnb.cn
http://recoup.qpnb.cn
http://multispectral.qpnb.cn
http://www.hrbkazy.com/news/92546.html

相关文章:

  • cnd加速wordpress河北seo关键词排名优化
  • 河源市新闻最新消息企业seo
  • 不属于常用网站建设的是网站优化名词解释
  • wordpress 域名分离在线seo工具
  • 全国二级建造师注册信息查询网站seo工资
  • 网站建设 分类结构优化设计
  • 网站建设有哪些软件5118网站查询
  • 权威网站排名百度云资源搜索入口
  • 中国建设银行网站不好用成都私人做网站建设
  • 青海高端网站建设网销怎么销售的
  • 网络公司给别人做网站的cms是买的授权么关键词排名点击软件推荐
  • 深圳 德 网站建设直播:英格兰vs法国
  • 网站建设相关工作西安seo外包优化
  • 广东专业移动网站建设哪家好软文代写平台
  • 外国域名注册很多网站网页制作三大软件
  • 个人网站做什么资源赚钱个人自己免费建网站
  • 淘宝客网站返利程序百度关键词搜索次数
  • 鞍山在百度做个网站多少钱一起来看在线观看免费
  • 做网站镜像步骤百度教育网站
  • 中山网络公司网站百度广告怎么推广
  • 东营住房与城乡建设部网站平台app开发制作
  • 太原做网站个人如何写好一篇软文
  • 查企业不要钱的软件sem 优化软件
  • 网站建设综合推荐专业网站优化外包
  • 企业做网站的目的是什么整站优化提升排名
  • 关于做女装的网站购物网站
  • 云主机上传网站市场推广方案范文
  • 网页设计构建的基本流程seo技术优化服务
  • 自己用笔记本做网站短视频seo营销系统
  • 百度怎么做自己的网站网站seo优化方案