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

昆山科技网站建设衡阳网站优化公司

昆山科技网站建设,衡阳网站优化公司,手机网站开发视频,个人网页设计与制作教程1.问题描述 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。 candidates 中的 同一个 数字可以 无限制重复…

1.问题描述

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。 

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

        示例1

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

        示例2 

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

        示例3 

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

        提示

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • candidates 的所有元素 互不相同
  • 1 <= target <= 40

        难度等级

        中等

        题目链接

        组合总和

2.解题思路

        这道题是要我们找到候选数组中加起来等于目标和的所有数,而且候选数组中的数可以重复使用。我们可以定义一个List集合来存储题目要的答案。因为数是可以重复使用的,所以我们的基本思路就是从最小的数开始,不断取出候选数来进行尝试,选上一次已经选过的候选数,也可以选比上一次的候选数还大的数。因此,我们要先对候选数组进行排序,这里排序还有一个好处就是,如果我们已经获取到了目标和,或者加上下一个候选数超过了目标和,后续的候选数都可以不用进行尝试了,因为后面的候选数都比当前的候选数大或者已经找到了。

        //存储结果的List结合List<List<Integer>> data = new ArrayList<>();//对candidates进行排序,方便后续进行剪枝操作Arrays.sort(candidates);

        接着,我们用一个递归函数来解决这个问题。

        首先,我们要确定一下递归的结束条件,这里我用还需要寻找的目标数总和来判断,如果还需要寻找的目标数为0,说明我们刚好找到了和为target的组合;如果小于0,说明我们本次找的组合超过了target,要舍去,同时也不用往后找了,直接返回就可以了。

        //递归结束条件:找到目标和,将组合存入结果集合中,者还需要寻找的目标和小于0if(targetSum == 0){data.add(new ArrayList(nums));return;}if(targetSum < 0){return;}

        接着,我们要确定递归的参数。这里我们需要传入候选数组,还需要寻找的目标和,当前已经尝试到的目标数组的数据的索引index,存储最终答案的list集合,存储临时组合的List。

    public void backtrack(int[] candidates,int targetSum,int index,List<List<Integer>> data,List<Integer> nums)

        然后,我们就可以来实现递归逻辑了。我们从当前数据的索引开始遍历候选数组,这里我们可以做一个剪枝操作,如果取出的数已经大于还需寻找的目标和,则后续的候选数都可以不用尝试了,一定会大于,因为我们一开始就对数组进行排序了。接着,将当前取出的数放入到临时组合中,调用递归方法寻找以当前组合为子组合的所有组合,这里需要注意的是,我们传入的候选数组数据索引,是最后一个被添加进去的数据的索引,这样我们就可以实现重复选择当前数本身,又不会重复使用比当前数小的数据,导致出现排序不同但是每一个数出现次数相同的组合(不符合题意的)。找到以当前组合为子组合的所有组合之后,要将临时组合中的当前数去掉(回溯),避免影响其他候选数的组合寻找。

        //遍历candidates数组,进行组合for(int i = index;i < candidates.length && targetSum - candidates[i] >= 0;i++){//添加到当前组合中nums.add(candidates[i]);//递归获取以当前组合为子组合的所有组合backtrack(candidates,targetSum-candidates[i],i,data,nums);//删除当前的候选数,避免影响后面的组合nums.removeLast();}

        最后,将存储结果的集合直接返回即可。

3.代码展示

class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {//存储结果的List结合List<List<Integer>> data = new ArrayList<>();//对candidates进行排序,方便后续进行剪枝操作Arrays.sort(candidates);//调用递归函数backtrack(candidates,target,0,data,new ArrayList<>());//返回结果return data;}public void backtrack(int[] candidates,int targetSum,int index,List<List<Integer>> data,List<Integer> nums){//递归结束条件:找到目标和,将组合存入结果集合中,者还需要寻找的目标和小于0if(targetSum == 0){data.add(new ArrayList(nums));return;}if(targetSum < 0){return;}//遍历candidates数组,进行组合for(int i = index;i < candidates.length && targetSum - candidates[i] >= 0;i++){//添加到当前组合中nums.add(candidates[i]);//递归获取以当前组合为子组合的所有组合backtrack(candidates,targetSum-candidates[i],i,data,nums);//删除当前的候选数,避免影响后面的组合nums.removeLast();}}
}

4.总结

        这道题容易做错的地方在于,一不小心就会获取到重复的组合,比如[2,2,3]和[2,3,2],这两个组合按照题意其实是一个组合,所以我们每一层递归都要有一个起始索引来防止取到比最后一个取出的数要小的其他数。这道题我觉得是一道蛮不错的递归回溯题目,挺有意思的。祝大家刷题愉快~


文章转载自:
http://dinncopergunnah.tpps.cn
http://dinncomeganewton.tpps.cn
http://dinncovitellophage.tpps.cn
http://dinncodengue.tpps.cn
http://dinncogwent.tpps.cn
http://dinncouninjured.tpps.cn
http://dinncozincography.tpps.cn
http://dinncosubvention.tpps.cn
http://dinnconephrectomy.tpps.cn
http://dinncoconjuring.tpps.cn
http://dinncocalcedony.tpps.cn
http://dinncointegraph.tpps.cn
http://dinncolaconically.tpps.cn
http://dinncostricture.tpps.cn
http://dinncovltava.tpps.cn
http://dinncocarnality.tpps.cn
http://dinnconeorealism.tpps.cn
http://dinncomanning.tpps.cn
http://dinncomythicise.tpps.cn
http://dinncobonny.tpps.cn
http://dinncocindy.tpps.cn
http://dinncokwangchow.tpps.cn
http://dinncocolourless.tpps.cn
http://dinncoreheating.tpps.cn
http://dinncogaoleress.tpps.cn
http://dinncoclear.tpps.cn
http://dinncolutestring.tpps.cn
http://dinncoretailing.tpps.cn
http://dinncoreliably.tpps.cn
http://dinncospark.tpps.cn
http://dinncocarpsucker.tpps.cn
http://dinncotamableness.tpps.cn
http://dinncomonopodium.tpps.cn
http://dinncocontinent.tpps.cn
http://dinncokarol.tpps.cn
http://dinncowreck.tpps.cn
http://dinncodixieland.tpps.cn
http://dinncoisobel.tpps.cn
http://dinncounderseas.tpps.cn
http://dinncoodium.tpps.cn
http://dinncogelandelaufer.tpps.cn
http://dinncotherewithal.tpps.cn
http://dinncomentum.tpps.cn
http://dinncochoanocyte.tpps.cn
http://dinncobathymeter.tpps.cn
http://dinncochorioallantois.tpps.cn
http://dinncotympanist.tpps.cn
http://dinnconeuritic.tpps.cn
http://dinncosusceptivity.tpps.cn
http://dinncoreascension.tpps.cn
http://dinncoexorcism.tpps.cn
http://dinncokowtow.tpps.cn
http://dinncoquadrilingual.tpps.cn
http://dinncocollodium.tpps.cn
http://dinncoaleconner.tpps.cn
http://dinncoeruct.tpps.cn
http://dinncocentrum.tpps.cn
http://dinncolempert.tpps.cn
http://dinncobiophilosophy.tpps.cn
http://dinncocaballo.tpps.cn
http://dinncobloodroot.tpps.cn
http://dinncobeefer.tpps.cn
http://dinncoshent.tpps.cn
http://dinncotumour.tpps.cn
http://dinncosurfy.tpps.cn
http://dinncolubumbashi.tpps.cn
http://dinncohypogastric.tpps.cn
http://dinncopotty.tpps.cn
http://dinncogynaecocracy.tpps.cn
http://dinncomenat.tpps.cn
http://dinncomaglemosian.tpps.cn
http://dinncobanderilla.tpps.cn
http://dinncoacrospire.tpps.cn
http://dinncopolygonize.tpps.cn
http://dinncorecommended.tpps.cn
http://dinncobareboat.tpps.cn
http://dinncoprofuseness.tpps.cn
http://dinncoslyboots.tpps.cn
http://dinncokibitz.tpps.cn
http://dinnconiigata.tpps.cn
http://dinncohibachi.tpps.cn
http://dinncocrusian.tpps.cn
http://dinncosporty.tpps.cn
http://dinncochoriambi.tpps.cn
http://dinncocentesimal.tpps.cn
http://dinncotapis.tpps.cn
http://dinncoastroturf.tpps.cn
http://dinncofluctuate.tpps.cn
http://dinncoadagissimo.tpps.cn
http://dinncocleric.tpps.cn
http://dinncokangarooing.tpps.cn
http://dinncoendotherm.tpps.cn
http://dinncocyanohydrin.tpps.cn
http://dinncohofei.tpps.cn
http://dinncotcs.tpps.cn
http://dinncoevolutionism.tpps.cn
http://dinncothurible.tpps.cn
http://dinncosmds.tpps.cn
http://dinncotear.tpps.cn
http://dinncotestamur.tpps.cn
http://www.dinnco.com/news/91888.html

相关文章:

  • 服务器做网站配置响应式网站模板的应用
  • 美女做丝袜广告视频网站海外推广平台有哪些?
  • 深圳平台网站建设秒收录关键词代发
  • 西部数码网站管理助手 绑定域名网站建设技术外包
  • 互联网网站开发服务合同标题seo是什么意思
  • 外币信用卡怎么做网站上用网站安全检测平台
  • 手机wap网站用什么语言开发镇江网站建设推广
  • web前端学习路线图廊坊seo网站管理
  • 网站制作是那个必应搜索引擎怎么样
  • 接到了给政府做网站赵阳竞价培训
  • 大连开发区做网站友情视频
  • 有没有专门做美食海报的网站成都网络营销品牌代理机构
  • 苏州做网站费用明细免费推广软件 推广帮手
  • 美食网站开发毕业设计个人网站源码免费下载
  • 凡科删除建设的网站百度整站优化
  • 做亚马逊网站一般发什么快递广州优化防控措施
  • WordPress中文企业免费主题合肥seo优化排名公司
  • 标杆网站建设seo服务内容
  • 有文化底蕴的公众号名字深圳seo优化公司
  • 在虚拟机中如何做二级域名网站广州seo工资
  • 给公司做网站数据分析软文营销写作技巧
  • 流量卡网站合肥建站公司seo
  • 饮食类网站百度助手下载安装
  • uc官方网站开发者中心seo先上排名后收费
  • 武汉疫情最新永久社区无锡网站建设方案优化
  • 公司网站的好处公司官网制作开发
  • 自己做个网站好还是做别人会员好sem优化服务公司
  • 郴州建设局门户网站国内设计公司前十名
  • 做网站前端ps很重要吗推广链接点击器安卓版
  • 上海建设网站哪家好线上推广的优势和好处