描述:给一个字符串s,查找它的最长的回文子串。s的长度不超过1000。

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
我是采用动态规划解决此题的。官方的solutions中提供了几种思路,包括我使用的DP。这里摘要如下: 思路1:
将s反转得到s',然后查找s和s'的最长公共子串substring,那么substring就是最长回文子串。
比如:s = "caba", s' = "abac" => substring = "aba"
但是存在特殊的情况如:
s = "abacdfgdcaba", s' = "abacdgfdcaba" => substring = "abacd"
可以看到substring并不是回文,这是因为s中存在子串s1和s2,而reverse(s1)=s2,但是s1本身并不是回文。
对这种情况,可以对比Index,检测反转后的substring是不是由反转前的substring得来。
至于查找s1和s2的最长公共子串,更优秀的方法可以参见:https://en.wikipedia.org/wiki/Longest_common_substring_problem 思路2:
直接暴力检索所有字符串是否回文,略过。 思路3(DP):
定义P(i,j)表示在s中索引i~j的字串是否为回文。则有:
P(i,j) = (P(i+1,j-1) and s[i] == s[j]),即当且仅当字符s[i] == s[j]且子串s(i+1,j-1)是回文时,s(i,j)是回文;
基础情形:
P(i,i) = true
P(i,i+1) = s[i] == s[i+1] 思路4:
任何一个回文子串都是由一个分隔点不断向两侧扩展相同字符得来的,因此只需要以每个字符或字符间隔点起始向两侧尽可能扩展,筛选最长的子串即可。此思路看起来似乎不怎么样,但是其时间复杂度也只有O(n的平方),而空间复杂度只有O(1)。 思路5:
详细见:https://articles.leetcode.com/longest-palindromic-substring-part-ii/

LeetCode-5:Longest Palindromic Substring(最长回文子字符串)的更多相关文章

  1. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  2. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  3. [leetcode]5. Longest Palindromic Substring最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  4. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  5. 1. Longest Palindromic Substring ( 最长回文子串 )

    要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...

  6. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  8. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  9. 【翻译】Longest Palindromic Substring 最长回文子串

    原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...

随机推荐

  1. widget 常用UI控件介绍

        一.单选框 单选框实例程序: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  2. svn:冲突(<<<<<<.mine ==== >>>>>>.xxxx)

    http://blog.csdn.net/u014000377/article/details/50605895 在svn更新文件时会产生有冲突的文件,一般有两种解决办法: 1.更新文件之前直接查看对 ...

  3. Rom Modified [Galaxy 3 Tested]

    1,Virtualbox虚拟机设置-数据空间注意这里不要勾选那个自动挂载,不然后面mount总会提示mount.vbox.. invalid argument. 2,进入ubuntu中,在终端下输入 ...

  4. Msfvenom 学习笔记与总结

    平台:Android,可用Payload: android/meterpreter/reverse_http Run a meterpreter server on Android. Tunnel c ...

  5. 我眼中的Oracle Database Software 和 Oracle Database

    我眼中的Oracle Database Software 和 Oracle Database 我喜欢用微软的office软件和word文档(确切的说是:自己写的word文档,能够把这个Word文档想象 ...

  6. vsftpd 自动安装脚本

    #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'cpy' import os import re import sys impo ...

  7. 【WPF学习笔记】之如何保存画面上新建的数据到数据库中并且删除画面上的数据和数据库的数据:动画系列之(五)

    ...... 承接系列四后续: 首先,我要在用户控件2中添加“保存”,“删除”按钮. XAML代码: <UserControl x:Class="User.uc_item" ...

  8. 关于一致/非一致代码段与TSS 关系的个人看法

    [0]概念定义 0.1)一致代码段: 简单理解,就是操作系统拿出来被共享的代码段,可以被低特权级的用户直接调用访问的代码, 但是特权级高的程序不允许访问特权级低的数据. 通常这些共享代码,是" ...

  9. ASP.NET动态网站制作(3)--css(2)

    前言:css分为四次课讲完,第一节课内容见ASP.NET动态网站制作(2)--css(1),接下来的内容会涉及到定位.浮动.盒子模型(第二次课).css的具体应用(第三次课).css3(第四次课).今 ...

  10. 【BZOJ4605】崂山白花蛇草水 权值线段树+kd-tree

    [BZOJ4605]崂山白花蛇草水 Description 神犇Aleph在SDOI Round2前立了一个flag:如果进了省队,就现场直播喝崂山白花蛇草水.凭借着神犇Aleph的实力,他轻松地进了 ...