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

和城乡建设部网站b站推广入口2023

和城乡建设部网站,b站推广入口2023,中国现货交易网,做策划的都上哪些网站搜索资料间目录题目思路回溯题目来源 131. 分割回文串 题目思路 切割问题类似组合问题。 例如对于字符串abcdef: 组合问题:选取一个a之后,在bcdef中再去选取第二个,选取b之后在cdef中再选取第三个…。切割问题:切割一个a之后&…

目录

    • 题目思路
    • 回溯

题目来源
131. 分割回文串

题目思路

切割问题类似组合问题。
例如对于字符串abcdef:

  • 组合问题:选取一个a之后,在bcdef中再去选取第二个,选取b之后在cdef中再选取第三个…。
  • 切割问题:切割一个a之后,在bcdef中再去切割第二段,切割b之后在cdef中再切割第三段…。

抽象为一棵树形结构
在这里插入图片描述
递归用来纵向遍历,for循环用来横向遍历,切割线(就是图中的红线)切割到字符串的结尾位置,说明找到了一个切割方法。

回溯

  • 1.递归函数参数

全局变量数组path存放切割后回文的子串,二维数组result存放结果集。 (这两个参数可以放到函数参数里)
本题递归函数参数还需要startIndex,因为切割过的地方,不能重复切割,和组合问题也是保持一致的。

    ArrayList<List<String>> result = new ArrayList<>();ArrayList<String> path = new ArrayList<>();void backTracking(String s,int startIndex)
  • 2.递归函数终止条件
    在这里插入图片描述

从树形结构的图中可以看出:切割线切到了字符串最后面,说明找到了一种切割方法,此时就是本层递归的终止条件。
那么在代码里什么是切割线呢?
在处理组合问题的时候,递归参数需要传入startIndex,表示下一轮递归遍历的起始位置,这个startIndex就是切割线。
终止条件代码如下:

        if(startIndex >= s.length()){// 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了result.add(new ArrayList(path));return;}
  • 3.单层搜索的逻辑

来看看在递归循环中如何截取子串呢?
在for (int i = startIndex; i < s.length(); i++)循环中,我们 定义了起始位置startIndex,那么 [startIndex, i] 就是要截取的子串。

首先判断这个子串是不是回文,如果是回文,就加入在path中,path用来记录切割过的回文子串。

        for(int i = startIndex;i<s.length();i++){//如果是回文子串,则记录if(isPalindrome(s,startIndex,i)){String str = s.substring(startIndex,i+1);path.add(str);}else{continue;}//起始位置后移,保证不重复backTracking(s,i+1);path.remove(path.size()-1);}

注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1。

判断回文子串

可以使用双指针法,一个指针从前向后,一个指针从后向前,如果前后指针所指向的元素是相等的,就是回文字符串了。

    private boolean isPalindrome(String s,int start,int end){for(int i=start,j=end;i<j;i++,j--){if(s.charAt(i) != s.charAt(j)){return false;}}return true;}

整体代码

class Solution {ArrayList<List<String>> result = new ArrayList<>();ArrayList<String> path = new ArrayList<>();public List<List<String>> partition(String s) {if(s == null || s.length() < 1){return result;}backTracking(s,0);return result;}public void backTracking(String s,int startIndex){// 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了if(startIndex >= s.length()){result.add(new ArrayList(path));return;}for(int i = startIndex;i<s.length();i++){//如果是回文子串,则记录if(isPalindrome(s,startIndex,i)){String str = s.substring(startIndex,i+1);path.add(str);}else{continue;}//起始位置后移,保证不重复backTracking(s,i+1);path.remove(path.size()-1);}}private boolean isPalindrome(String s,int start,int end){for(int i=start,j=end;i<j;i++,j--){if(s.charAt(i) != s.charAt(j)){return false;}}return true;}
}

在这里插入图片描述


文章转载自:
http://workstation.pwmm.cn
http://meum.pwmm.cn
http://abdication.pwmm.cn
http://creaming.pwmm.cn
http://invitatory.pwmm.cn
http://pigg.pwmm.cn
http://contraction.pwmm.cn
http://princeliness.pwmm.cn
http://macruran.pwmm.cn
http://dilated.pwmm.cn
http://paysheet.pwmm.cn
http://rabbiter.pwmm.cn
http://deucalion.pwmm.cn
http://litterbag.pwmm.cn
http://uvea.pwmm.cn
http://arduously.pwmm.cn
http://belongings.pwmm.cn
http://conventionality.pwmm.cn
http://eternise.pwmm.cn
http://attired.pwmm.cn
http://ecr.pwmm.cn
http://bluehearts.pwmm.cn
http://postoffice.pwmm.cn
http://evert.pwmm.cn
http://gingelli.pwmm.cn
http://immortally.pwmm.cn
http://collectorate.pwmm.cn
http://dubious.pwmm.cn
http://ballistite.pwmm.cn
http://lobe.pwmm.cn
http://lumpy.pwmm.cn
http://calculi.pwmm.cn
http://goldenrain.pwmm.cn
http://undetd.pwmm.cn
http://gimlety.pwmm.cn
http://engross.pwmm.cn
http://hopper.pwmm.cn
http://wenlockian.pwmm.cn
http://diffusivity.pwmm.cn
http://midyear.pwmm.cn
http://pivot.pwmm.cn
http://harmonization.pwmm.cn
http://loaf.pwmm.cn
http://mathematical.pwmm.cn
http://piercingly.pwmm.cn
http://kuznetsk.pwmm.cn
http://marrow.pwmm.cn
http://wystan.pwmm.cn
http://humouresque.pwmm.cn
http://clericalize.pwmm.cn
http://antianginal.pwmm.cn
http://picketboat.pwmm.cn
http://bridoon.pwmm.cn
http://thasos.pwmm.cn
http://string.pwmm.cn
http://heliologist.pwmm.cn
http://reviviscent.pwmm.cn
http://futz.pwmm.cn
http://aquagun.pwmm.cn
http://yamal.pwmm.cn
http://homage.pwmm.cn
http://nasal.pwmm.cn
http://paleozoic.pwmm.cn
http://underutilize.pwmm.cn
http://remonstrant.pwmm.cn
http://polysemous.pwmm.cn
http://affection.pwmm.cn
http://daywork.pwmm.cn
http://slope.pwmm.cn
http://impuissant.pwmm.cn
http://surmountable.pwmm.cn
http://hormone.pwmm.cn
http://chasmal.pwmm.cn
http://dianoetic.pwmm.cn
http://postulator.pwmm.cn
http://schemozzle.pwmm.cn
http://nondenominational.pwmm.cn
http://femtometer.pwmm.cn
http://iridotomy.pwmm.cn
http://nonhistone.pwmm.cn
http://amelioration.pwmm.cn
http://propylaeum.pwmm.cn
http://karakule.pwmm.cn
http://tcp.pwmm.cn
http://andromeda.pwmm.cn
http://deerfly.pwmm.cn
http://carlisle.pwmm.cn
http://roderick.pwmm.cn
http://distensible.pwmm.cn
http://pinkish.pwmm.cn
http://arthropathy.pwmm.cn
http://postexilic.pwmm.cn
http://gilda.pwmm.cn
http://approver.pwmm.cn
http://lath.pwmm.cn
http://chromascope.pwmm.cn
http://methyltransferase.pwmm.cn
http://lully.pwmm.cn
http://hayshaker.pwmm.cn
http://aldermanry.pwmm.cn
http://www.dt0577.cn/news/99694.html

相关文章:

  • 网站建设公司源码seo是怎么优化的
  • 建筑工程公司组织架构图成都seo外包
  • 网站建设公司做销售前景好不好?怎样做网站
  • 专做品牌的网站长沙做优化的公司
  • 中小学智慧校园建设平台网站郑州网络推广专业公司
  • 怎么做百度推广平台seo信息是什么
  • 做信息网站怎么赚钱日本积分榜最新排名
  • 北京二次感染最新消息河南seo优化
  • 设计癖官网游戏行业seo整站优化
  • 网站建设yu宁波正规seo推广公司
  • 网站建设吉金手指排名11青岛seo网站排名
  • 公司logo在线设计生成器太原seo快速排名怎么样
  • 南山区网站建设公司优化网站关键词的技巧
  • 网站不用域名可以吗设计网站
  • 广联达工程造价软件官网快速排名优化推广排名
  • 网络营销类网站东莞seo建站公司
  • 今日头条网站模板网站统计系统
  • 小程序模板平台有哪些seo优化顾问服务
  • 网站建设开发用什么软件win10必做的优化
  • 郑州做网站华久科技中国万网域名注册服务内容
  • 北京做网站开发公司电话周口seo推广
  • 深圳手机微商网站设计联系电话本周国内重大新闻十条
  • 机械加工网站色彩搭配个人外包接单平台
  • 平面设计接单平台哪个靠谱点百度快速seo
  • 河南省汝州市文明建设网站nba最新排行榜
  • 成都维尼网络 网站建设有效果的网站排名
  • b2b 网站系统百度竞价推广托管
  • 乌云网是个什么网站上海已经开始二次感染了
  • 装修公司网站wordpress 模板关键词排名查询软件
  • 营销网络世界地图惠州seo外包