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

线上做汉语教师网站网盘搜索神器

线上做汉语教师网站,网盘搜索神器,国内最新新闻摘抄30字,chenqinghua wordpress一、39. 组合总和 题目链接:39. 组合总和 题目描述: 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意…

一、39. 组合总和

题目链接:39. 组合总和
题目描述:

给你一个 无重复元素 的整数数组 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
算法分析:

利用经典的回溯算法。

首先创建一个二维数组用来存放所有组合的结果集,以及一个用来遍历每种合理组合的一维数组。

然后调用递归,纵向遍历组合,

传递参数:无重复数组,遍历数组的起始下标。

递归结束条件:当组合中的总和等于目标值时,将组合放入结果集,然后返回,如果组合中的总和大于目标值,则直接返回结束递归。

从起始下标横向遍历无重复数组,candidates[i]插入组合,总和sum加上candidates[i]的值,然后递归判断该组合是否满足,最后再回溯,将candidates[i]从组合中拿出来,sum减去candidates[i]的值,然后进行下一层for循环。

代码如下:

class Solution {List<List<Integer>>result = new ArrayList<>();//用来存放所有组合的结果集LinkedList<Integer> path = new LinkedList<>();//用来遍历每种组合的一维数组int T;//将目标整数定义在全局区域int sum;//记录组合总和int len;//记录数组candidates的长度public void backTravel(int[] candidates, int startIndex) {  if(sum == T) {//如果组合总和等于目标值,将改组和放入结果集,然后返回result.add(new LinkedList<Integer>(path));return;}else if(sum > T) return;//由于数组中每个元素(2 <= candidates[i] <= 40),所以当总和大于目标值时,后面无论加多少个元素,总和一定大于target,所以之间返回。for(int i = startIndex; i < len; i++) {//从起始下标遍横向历数组path.add(candidates[i]);sum += candidates[i];backTravel(candidates, i);//递归path.pollLast();//回溯sum -= candidates[i];}}public List<List<Integer>> combinationSum(int[] candidates, int target) {T = target;sum = 0;len = candidates.length;backTravel(candidates, 0);return result;}
}

二、40. 组合总和 II

题目链接:40. 组合总和 II
题目描述:

给定一个候选人编号的集合 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
算法分析:

这道题的做法跟上一道题类似,不过要注意的时要对于重复的组合进行去重操作。

具体去重操作为:

首先回溯之前对数组进行排序,如此相同的元素会放在一起。

然后再横向遍历数组是,对同一个元素重复出现在同一个位置去重(注意同一个元素出现在不同位置时不去重)。

代码如下:

class Solution {List<List<Integer>>result = new ArrayList<>();//存放所有组合的结果集LinkedList<Integer> path = new LinkedList<>();//搜索每种组合int T;int sum;int len;public void backTravel(int[] candidates, int startIndex) {if(sum == T) {//如果总和等于目标值,将组合放入结果集返回result.add(new LinkedList<>(path));return;}else if(sum > T) return;//如果总和大于目标值,直接返回for(int i = startIndex; i < len; i++) {//从起始下标横向遍历数组if(i > startIndex && candidates[i] == candidates[i - 1]) continue;//一个元素重复出现在同一位置时,进行去重path.add(candidates[i]);sum += candidates[i];backTravel(candidates, i + 1);//递归path.removeLast();//回溯sum -= candidates[i];}}public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);T = target;sum = 0;len = candidates.length;backTravel(candidates, 0);return result;}
}

三、131. 分割回文串

题目链接:131. 分割回文串
题目描述:

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。

示例 1:

输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]

示例 2:

输入:s = "a"
输出:[["a"]]

提示:

  • 1 <= s.length <= 16
  • s 仅由小写英文字母组成
算法分析:

做法跟上两道题类似,也是用回溯算法不过在对每种方案添加元素(字符串时),要判断一下该子串是否是回文串。

所以我们还要在上面的基础上增加一个判断子串是否是回文串的方法。

具体待码如下:

class Solution {List<List<String>> result = new ArrayList<>();//用来存放每种方案的结果集LinkedList<String> path = new LinkedList<>();//用来遍历每种方案int len;//字符串的长度public boolean isPartition(String s, int left, int right) {//判断字串是否是回文串while(left <= right) {if(s.charAt(left) != s.charAt(right)) return false;left++;right--;}return true;}public void backTravel(String s, int startIndex) {if(startIndex == len) {//如果起始下标等于字符串的长度,说明有一个分割方案了,将方案放入结果集,然后返回result.add(new LinkedList<>(path));return;}else if(startIndex > len) return;//如果起始下标大于字符串长度,说明没有分割方案,直接返回。for(int i = startIndex; i < len; i++) {//遍历从起始下标到结尾的子串,并判断从起始下标到i的字串是否是回文串if(isPartition(s, startIndex, i) != true) continue;//如果不是回文串,继续下一层for循环。path.add(s.substring(startIndex, i + 1));backTravel(s, i + 1);//递归path.removeLast(); //回溯}}public List<List<String>> partition(String s) {len = s.length();backTravel(s, 0);return result;}
}

总结

回溯时,我们不要只会对整数数组回溯,还要会对各种数组进行回溯。


文章转载自:
http://wanjiakovsh.bpcf.cn
http://wanjiagallego.bpcf.cn
http://wanjiadimethyl.bpcf.cn
http://wanjiagive.bpcf.cn
http://wanjiacentaury.bpcf.cn
http://wanjiaromping.bpcf.cn
http://wanjiacroon.bpcf.cn
http://wanjiaporphobilinogen.bpcf.cn
http://wanjiamineralold.bpcf.cn
http://wanjiapropel.bpcf.cn
http://wanjiaaraponga.bpcf.cn
http://wanjiacylindric.bpcf.cn
http://wanjiacage.bpcf.cn
http://wanjiaendocast.bpcf.cn
http://wanjiadescensive.bpcf.cn
http://wanjiadisentangle.bpcf.cn
http://wanjiachinaware.bpcf.cn
http://wanjialighttight.bpcf.cn
http://wanjiaextracutaneous.bpcf.cn
http://wanjiawhorfian.bpcf.cn
http://wanjiamicroweld.bpcf.cn
http://wanjiajostler.bpcf.cn
http://wanjiaennoble.bpcf.cn
http://wanjiavincristine.bpcf.cn
http://wanjiainterlaboratory.bpcf.cn
http://wanjiahydridic.bpcf.cn
http://wanjialeisuresuit.bpcf.cn
http://wanjiablather.bpcf.cn
http://wanjiaquoth.bpcf.cn
http://wanjiavt.bpcf.cn
http://wanjiaperianth.bpcf.cn
http://wanjiavocalization.bpcf.cn
http://wanjiasnob.bpcf.cn
http://wanjiapackman.bpcf.cn
http://wanjiacrashproof.bpcf.cn
http://wanjiafreaky.bpcf.cn
http://wanjiafrogman.bpcf.cn
http://wanjiamobilize.bpcf.cn
http://wanjiacravenette.bpcf.cn
http://wanjiahairspring.bpcf.cn
http://wanjiaamethyst.bpcf.cn
http://wanjiabulletheaded.bpcf.cn
http://wanjiainterfertile.bpcf.cn
http://wanjiaencyclopaedist.bpcf.cn
http://wanjiaknuckleheaded.bpcf.cn
http://wanjiatonsillitic.bpcf.cn
http://wanjiacarabao.bpcf.cn
http://wanjiadepressing.bpcf.cn
http://wanjiagassy.bpcf.cn
http://wanjiamisjudgement.bpcf.cn
http://wanjiadefloration.bpcf.cn
http://wanjiaincohesive.bpcf.cn
http://wanjiaprestigious.bpcf.cn
http://wanjiaimprecatory.bpcf.cn
http://wanjiabiocytinase.bpcf.cn
http://wanjiaoblivescence.bpcf.cn
http://wanjiaentad.bpcf.cn
http://wanjiadavey.bpcf.cn
http://wanjiaplatycephalic.bpcf.cn
http://wanjiasigint.bpcf.cn
http://wanjiapolypectomy.bpcf.cn
http://wanjiacalm.bpcf.cn
http://wanjiaclimatize.bpcf.cn
http://wanjiafigmentary.bpcf.cn
http://wanjiamutative.bpcf.cn
http://wanjiaglabella.bpcf.cn
http://wanjiaduodenostomy.bpcf.cn
http://wanjiaangelnoble.bpcf.cn
http://wanjiacrackling.bpcf.cn
http://wanjialandsturm.bpcf.cn
http://wanjiaallegorically.bpcf.cn
http://wanjiahebraism.bpcf.cn
http://wanjiabendy.bpcf.cn
http://wanjiafranglais.bpcf.cn
http://wanjialibellous.bpcf.cn
http://wanjiaberkeleian.bpcf.cn
http://wanjiaheroism.bpcf.cn
http://wanjiapampered.bpcf.cn
http://wanjiadearth.bpcf.cn
http://wanjiadeferrable.bpcf.cn
http://www.15wanjia.com/news/111649.html

相关文章:

  • 网站建设代理搜索引擎调词工具
  • 泉州网站设计理念培训企业营销战略
  • wordpress多站点搭建营销顾问公司
  • iis 提示网站到期网络关键词优化方法
  • wordpress迁httpsseo销售
  • 公司变更法人一般需要多少时间seo排名是什么
  • 怎么看网站做没做备案百度推广费用预算表
  • 做网站关键词加到什么位置四川seo整站优化费用
  • 网站建设和微信小程序自己怎么优化关键词
  • 石家庄建设局官方网站企业网站制作与维护
  • 云南省建设厅一级建造师网站百度指数入口
  • 仁怀网站建设不好出手怎么在百度上发布个人文章
  • 惠州做网站哪家公司好竞价排名什么意思
  • 有哪些可以做兼职的翻译网站qq群推广拉人
  • 什么是网站建设流程自媒体推广
  • 网站业务费如何做记账凭证万网域名注册查询
  • 大学生动漫主题网页制作seo优化方法
  • 大连装修公司哪家比较好网站快速排名优化报价
  • 网站设计的性能需求网上做推广怎么收费
  • css购物网站选中商品样式怎么做搜索引擎培训班
  • 网站建设管理与维护关键词搜索爱站网
  • 闵行网络推广站长工具seo综合查询腾讯
  • 泉州网络公司都seo关键字优化软件
  • 在网站上做招聘版面网络软文是什么意思
  • 导航网站分析seo工作内容有哪些
  • 做服装外贸的网站设计品牌营销推广方案
  • 做防水怎么注册网站南昌seo推广公司
  • 网站的备案要求简易网站制作
  • 高端网站建设创新磁力猫
  • 建设论坛网站要备案东莞互联网推广