一 字符串中的最大回文串(第5题)

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of sis 1000.

Example:

  1. Input: "babad"
  2.  
  3. Output: "bab"
  4.  
  5. Note: "aba" is also a valid answer.

Example:

  1. Input: "cbbd"
  2.  
  3. Output: "bb"

1. 我的解法(accepted): 中心扩展

思路: 回文即代表有中心,一次遍历中,对于每个位置上的数字求一下最大的回文串即可,初始处理剪枝,合并掉同样的元素,如xxxxaaaxxxx,先常量级别把a合并掉; 遍历时候再次剪枝,遍历过的有一个maxLen,如果即将遍历的元素最大可能回文长度都不可能超过maxLen,不再遍历。

  1. public class Test1218 {
  2.  
  3. public static void main(String[] args) {
  4. String str = "ababababa";
  5. System.out.println(longestPalindrome(str));
  6.  
  7. }
  8.  
  9. public static String longestPalindrome(String s) {
  10. if (s == null) {
  11. return null;
  12. }
  13. char[] chars = s.toCharArray();
  14. int length = chars.length;
  15. int maxLen = 0;
  16. String maxStr = "";
  17.  
  18. for (int i = 0; i < length; i++) {
  19.  
  20. // cut branch
  21. int possibleLength = getMaxPossibleLength(i, length);
  22. if (possibleLength < maxLen) {
  23. continue;
  24. }
  25.  
  26. String maxStrTmp = getMaxStrByIndex(i, chars);
  27. if (maxLen < maxStrTmp.length()) {
  28. maxLen = maxStrTmp.length();
  29. maxStr = maxStrTmp;
  30. }
  31. }
  32. return maxStr;
  33. }
  34.  
  35. private static int getMaxPossibleLength(int index, int length) {
  36. int head = 0;
  37. int tail = length - 1;
  38. if (index == head || index == tail) {
  39. return 1;
  40. }
  41. int result1 = index - head;
  42. int result2 = tail - index;
  43.  
  44. int min = result1 <= result2 ? result1 : result2;
  45. return min * 2 + 1;
  46. }
  47.  
  48. private static String getMaxStrByIndex(int index, char[] chars) {
  49. StringBuilder sb = new StringBuilder(String.valueOf(chars[index]));
  50. int length = chars.length;
  51. int head = index - 1;
  52. int tail = index + 1;
  53.  
  54. // middle deal
  55. while (true) {
  56. if (head >= 0 && chars[index] == chars[head]) {
  57. sb.insert(0, String.valueOf(chars[head--]));
  58. } else if (tail <= length - 1 && chars[index] == chars[tail]) {
  59. sb.append(String.valueOf(chars[tail++]));
  60. } else {
  61. break;
  62. }
  63. }
  64.  
  65. // besides deal
  66. while (true) {
  67. if (head < 0 || tail > length - 1) {
  68. break;
  69. }
  70. if (head >= 0 && tail <= length - 1 && chars[head] == chars[tail]) {
  71. sb.insert(0, String.valueOf(chars[head--]));
  72. sb.append(String.valueOf(chars[tail++]));
  73. continue;
  74. }
  75. break;
  76.  
  77. }
  78. return sb.toString();
  79. }
  80. }

2. dp解法

思路: 设 p[i][j] 代表下标从i至j的子字符串是否是回文串,取值为boolean

转移方程 p[i][j] = p[i+1][j-1] && chars[i] == chars[j]

初始化的状态为 p[i][i] = true    p[i][i+1] = chars[i] == chars[i+1]

看下面手绘图理解一下,打勾的对角线p[i][i] 恒为true, 只有这一列是不够状态转移的,因为按照转移方程,必须要图示的↗️方向递推,那么要需要有圆圈的一列初始化,这列对应的是p[i][i+1]。 剩下的递推即可,比如图中的箭头栗子

leetcode 算法整理的更多相关文章

  1. Leetcode——回溯法常考算法整理

    Leetcode--回溯法常考算法整理 Preface Leetcode--回溯法常考算法整理 Definition Why & When to Use Backtrakcing How to ...

  2. Leetcode——二叉树常考算法整理

    二叉树常考算法整理 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路.也欢迎大家来给我的Github的Leetcode算法项目点star呀~~ 二叉树常考算法 ...

  3. BFS与DFS常考算法整理

    BFS与DFS常考算法整理 Preface BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或 ...

  4. leetcode算法: Find Bottom Left Tree Value

    leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...

  5. LeetCode算法题-Subdomain Visit Count(Java实现)

    这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...

  6. LeetCode算法题-Number of Lines To Write String(Java实现)

    这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...

  7. LeetCode算法题-Unique Morse Code Words(Java实现)

    这是悦乐书的第318次更新,第339篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第186题(顺位题号是804).国际莫尔斯电码定义了一种标准编码,其中每个字母映射到一系 ...

  8. LeetCode算法题-Rotate String(Java实现)

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  9. LeetCode算法题-Rotated Digits(Java实现)

    这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...

随机推荐

  1. .net Core使用 MongoDB

    1.安装mogodb windows版本下载地址:https://www.mongodb.com/download-center/v2/community 查看mongod.conf文件,找到绑定的I ...

  2. H5的pushState与replaceState的用法

    一.简介 HTML5引入了 history.pushState()和 history.replaceState()方法,它们分别可以添加和修改历史记录条目.这些方法通常与window.onpopsta ...

  3. 字符串匹配(KMP&BF)

    字符串匹配   题目描述 设计一个程序,从一个主字符串中查找一个子字符串在主串中第一次出现的位置.主串和子串的长度不超过100.如果找不到,则输出-1. 程序输入说明 第一行输入一个整数N,说明需要进 ...

  4. VUE -- Identifier 'n_type' is not in camel case

    Identifier 'n_type' is not in camel case 参数名的 `_` 去掉就好了

  5. python将py文件转换为pyc

    python -m py_compile lib/ylpy.py python -m py_compile lib/ylpy.py python 一个.py文件如何调用另一个.py文件中的类和函数 A ...

  6. JAVA基于File的基本的增删改查

    直接上代码 public class TestFile { /** * 创建目录 * @param filename 路径 */ public static void createFile(Strin ...

  7. python3 django项目从项目中导出依赖包

    1. 在项目的根目录中使用mac终端执行命令, pip3 freeze > requirements.txt #requirements.txt只是个名字可以随便起,一般默认为requireme ...

  8. appStore上传苹果应用程序软件发布

    首先确定帐号是否能发布, https://developer.apple.com/account,如果你打开Provisioning Portal,然后点击DisTribution(1)图中加号是灰色 ...

  9. Java同步数据结构之ConcurrentLinkedQueue

    前言 前面介绍的Queue都是通过Lock锁实现的阻塞队列,今天介绍一种非阻塞队列ConcurrentLinkedQueue,所谓非阻塞,其实就是通过CAS代替加锁来实现的高效的非阻塞队列.当许多线程 ...

  10. 设置Win10默认启动的Linux子系统版本,启动指定Linux发行版

    设置Win10默认启动的Linux子系统版本,启动指定Linux发行版   MS酋长一年前已经与大家分享了启用“适用于Linux的Windows子系统(WSL)”的方法,但当时所能安装的只有由Cano ...