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

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

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

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

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

 public class Test1218 {

     public static void main(String[] args) {
String str = "ababababa";
System.out.println(longestPalindrome(str)); } public static String longestPalindrome(String s) {
if (s == null) {
return null;
}
char[] chars = s.toCharArray();
int length = chars.length;
int maxLen = 0;
String maxStr = ""; for (int i = 0; i < length; i++) { // cut branch
int possibleLength = getMaxPossibleLength(i, length);
if (possibleLength < maxLen) {
continue;
} String maxStrTmp = getMaxStrByIndex(i, chars);
if (maxLen < maxStrTmp.length()) {
maxLen = maxStrTmp.length();
maxStr = maxStrTmp;
}
}
return maxStr;
} private static int getMaxPossibleLength(int index, int length) {
int head = 0;
int tail = length - 1;
if (index == head || index == tail) {
return 1;
}
int result1 = index - head;
int result2 = tail - index; int min = result1 <= result2 ? result1 : result2;
return min * 2 + 1;
} private static String getMaxStrByIndex(int index, char[] chars) {
StringBuilder sb = new StringBuilder(String.valueOf(chars[index]));
int length = chars.length;
int head = index - 1;
int tail = index + 1; // middle deal
while (true) {
if (head >= 0 && chars[index] == chars[head]) {
sb.insert(0, String.valueOf(chars[head--]));
} else if (tail <= length - 1 && chars[index] == chars[tail]) {
sb.append(String.valueOf(chars[tail++]));
} else {
break;
}
} // besides deal
while (true) {
if (head < 0 || tail > length - 1) {
break;
}
if (head >= 0 && tail <= length - 1 && chars[head] == chars[tail]) {
sb.insert(0, String.valueOf(chars[head--]));
sb.append(String.valueOf(chars[tail++]));
continue;
}
break; }
return sb.toString();
}
}

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. [Luogu] 染色

    https://www.luogu.org/problemnew/show/P2486 qizha 为什么会wa #include <cstdio> #include <cmath& ...

  2. Django-常用异常

    1 from rest_framework.authentication import BasicAuthentication raise AuthenticationFailed(res.dict) ...

  3. dubbo——高可用性

    一.zookeeper宕机 zookeeper注册中心宕机,还可以消费dubbo暴露的服务 健壮性: 监控中心宕掉不影响使用,只是丢失部分采样数据 数据库宕掉后,注册中心仍能通过缓存提供服务列表查询, ...

  4. UOJ #164 [清华集训2015]V (线段树)

    题目链接 http://uoj.ac/problem/164 题解 神仙线段树题. 首先赋值操作可以等价于减掉正无穷再加上\(x\). 假设某个位置从前到后的操作序列是: \(x_1,x_2,..., ...

  5. call()与构造函数的运用

    一.简介 call()和apply()方法是所有函数体的固有属性,可以在指定作用域下调用函数.这里面有两层意思:1.可以在另外的作用域下调用函数:2.当函数体是是构造函数时,call()方法能达到类之 ...

  6. suduku

    github地址 PSP: PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 30 30 Estimate 估 ...

  7. php中strlen()和mb_strlen()函数

    php中strlen()和mb_strlen()函数 一.总结 一句话总结: mb_strlen()函数 的作用是 通过不同的编码计算字符串的长度: 比如 echo mb_strlen('中文a字1符 ...

  8. springmvc配置jackson时遇到的一些问题

    在没接触springmvc之前我们在servlet中想返回前台json数据时,都是自定义一个JSONObject和JSONArray,然后调用response.getWriter()对象的方法返回js ...

  9. Flutter移动电商实战 --(35)列表页_上拉加载更多制作

    右侧列表上拉加载配合类别的切换 上拉加载需要一个page参数,当点击大类或者小类的时候,这个page就要变成1 provide内定义参数 首先我们需要定义一个page的变量 下图是我们之前在首页的时候 ...

  10. linux shell中如何让$就表示为$呢?

    答: 在$前加转义符\ 如: \$