Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)


给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

返回 s 所有可能的分割方案。

示例:

  1. 输入: "aab"
  2. 输出:
  3. [
  4. ["aa","b"],
  5. ["a","a","b"]
  6. ]

  1. 分析:给定一个字符串,要求分割,并且要求分割出来的所有的串是回文串。
    利用回溯,每次dfs分两个分支 1、不分割继续往下 2、分割后在往下
  2.  
  3. 代码中dfs函数str存放字符串,n存放总长度,step存放当前位置,now是现在已经分割出来的字符串,
    remain是字符串剩余还没有分割的,list里装的是分割出来的now
  4.  
  5. 另外加了一个函数来判断,是否为回文串。
  1. class Solution {
  2. List<List<String>> ans = new ArrayList<>();
  3.  
  4. public List<List<String>> partition(String s) {
  5. if (s.length() == 0 || s == null) {
  6. return ans;
  7. }
  8. dfs(s,s.length(),0,"",s,new ArrayList<String>());
  9. return ans;
  10. }
  11.  
  12. public void dfs(String str, int n, int step, String now, String remain,
  13. ArrayList<String> list) {
  14.  
  15. if (n == step) {
  16.  
  17. if (!now.equals("") && isPalindrome(now)) {
  18. list.add(now);
  19. ans.add(new ArrayList<>(list));
  20. list.remove(list.size() - 1);
  21. }else if(!remain.equals("") && isPalindrome(remain)){
  22. list.add(remain);
  23. ans.add(new ArrayList<>(list));
  24. list.remove(list.size() - 1);
  25. }else if(list.size()!=0 && String.join("",list).equals(str)){
  26.  
  27. ans.add(new ArrayList<>(list));
  28. }
  29.  
  30. return;
  31. }
  32.  
  33. dfs(str, n, step + 1, now + str.charAt(step),
  34. remain.replaceFirst(str.charAt(step) + "", ""), list);
  35.  
  36. if (!now.equals("") && isPalindrome(now)) {
  37. // System.out.println(step+" "+now + " " + remain);
  38. list.add(now);
  39. dfs(str, n, step + 1, remain.charAt(0)+"", remain.replaceFirst(remain.charAt(0)+"", ""), list);
  40. list.remove(list.size() - 1);
  41.  
  42. }
  43.  
  44. }
  45.  
  46. public boolean isPalindrome(String str) {
  47.  
  48. for (int i = str.length()-1, j = 0; i>=0 && j<=str.length() && i!=j; i--, j++) {
  49. if (str.charAt(i) != str.charAt(j)) {
  50. return false;
  51. }
  52. }
  53. return true;
  54. }
  55. }

Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)的更多相关文章

  1. LeetCode 131. 分割回文串(Palindrome Partitioning)

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

  2. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

  3. [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. Java实现 LeetCode 131 分割回文串

    131. 分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa ...

  5. Leetcode 131.分割回文串

    分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...

  6. LeetCode 131. 分割回文串(Palindrome Partitioning)

    题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...

  7. 131. 分割回文串 javascript实现

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...

  8. [LeetCode] 132. 分割回文串 II

    题目链接 : https://leetcode-cn.com/problems/palindrome-partitioning-ii/ 题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子 ...

  9. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

随机推荐

  1. layui上传Excel更新数据并下载

    前言: 最近做项目遇到了一个需求,上传Excel获取数据更新Excel文档,并直接返回更新完的Excel到前端下载:其实需求并没有什么问题,关键是前端用到的是layui上传组件(layui.uploa ...

  2. shiro创建配置对象

    在执行 Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(&quo ...

  3. pycharm与monkeyrunner测试

      操作命令: 导包: import sysfrom com.android.monkeyrunner import MonkeyRunner,MonkeyDevice  device=MonkeyR ...

  4. 用机智云做PWM占空比控制电机,物联网智能家居应用

      因为是新申请的博客,所以申请了总想往里面加点东西,所以把我之前在机智云写的帖子复制了过来 (各位抱歉,由于之前上传的文件可能有错误,之前上传的文件PWM不能用,那么我又重新上传了一个文件,这个文件 ...

  5. 自定义SWT控件三之搜索功能下拉框

    3.搜索功能下拉弹出框 package com.view.control.select; import java.util.ArrayList; import java.util.LinkedList ...

  6. Kubernetes容器集群管理环境 - 完整部署(中篇)

    接着Kubernetes容器集群管理环境 - 完整部署(上篇)继续往下部署: 八.部署master节点master节点的kube-apiserver.kube-scheduler 和 kube-con ...

  7. Linux基础文件打包

    一.打包与解压 (一).打包压缩 [root@linux ~]# tar -czf etc1.tar.gz /etc //-z 调用gzip [root@linux ~]# tar -cjf etc2 ...

  8. 设计模式:与SpringMVC底层息息相关的适配器模式

    目录 前言 适配器模式 1.定义 2.UML类图 3.实战例子 4.总结 SpringMVC底层的适配器模式 参考 前言 适配器模式是最为普遍的设计模式之一,它不仅广泛应用于代码开发,在日常生活里也很 ...

  9. 二叉查找树(查找、插入、删除)——C语言

    二叉查找树 二叉查找树(BST:Binary Search Tree)是一种特殊的二叉树,它改善了二叉树节点查找的效率.二叉查找树有以下性质: (1)若左子树不空,则左子树上所有节点的值均小于它的根节 ...

  10. [Pulsar系列] 10分钟学会Pulsar消息系统概念

    Apache Pulsar Pulsar是一个支持多租户的.高性能的服务与服务之间消息通讯的解决方案,最初由雅虎开发,现在由Apache软件基金会管理. Pulsar的主要特性如下: Pulsar实例 ...