题目:https://leetcode.com/problems/longest-palindromic-substring/

算法分析

这道题的解法有三种:暴力法、动态规划、Manacher算法。三种方法的时间复杂度分别为O(n3),O(n2),O(n)。暴力法过于简单粗暴,Manacher算法又有点不好理解,所以,这里只介绍动态规划法。对于O(n2)的时间复杂度,也是可以接受的吧。如果,你想追求5G的极速享受,请转接http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

既然要利用动态规划,首要任务就是要找到状态转移方程。乍看此题,哪里来的状态转移。所以,我们需要构建状态。

这里,我们要定义一个二维数组Matrix[len(str)][len(str)],用来存储状态。对于为什么这么定义,我只能说记住就行了,以后类似的题目都是可以套用的。

下面,解释一下状态的含义。Matrix[i][j]表示的是字符串str从第i位置到第j位置是否为回文子串,如果是,则存储True,如果不是,则存储False。

这样状态转移方程就可以写出来了:

Matrix[i][j]={True,False,     Matrix[i+1][j−1] and str[i]=str[j]     else

最后,需要注意一些特殊状态的赋值:

Matrix[i][i]=True

Matrix[i][i+1]=True when str[i]=str[i+1]

这样,状态转移方程就建立完成了。我们只需要把这个矩阵填充完,就可以找到最大的回文子串了。

怎么找最大回文子串?你可以在程序开头定义两个变量,分别记录最大回文子串的长度和开始位置,这样在每次更新Matrix矩阵的过程中,比较当前找到的最大回文子串的长度与变量里记录的最大长度,然后决定是否更新,如果更新,就把最大回文子串的开始位置也更新一下。这样,当整个矩阵填充完,相应的最大回文子串也就找到了。

代码实现

动态规划:

class Solution(object):
def longestPalindrome(self, s1):
"""
:type s: str
:rtype: str
"""
length = len(s1)
max = 0
start = 0
matrix = [[True if i == j else False for i in range(length)] for j in range(length)]
for i in range(length-1):
if s1[i] == s1[i+1]:
matrix[i][i+1] = True
max = 1
start = i
for step in range(2, length):
for i in range(length-step):
if s1[i] == s1[i+step] and matrix[i+1][i+step-1]:
matrix[i][i+step] = True
if max < step:
max = step
start = i
return s1[start:start+max+1]

Manacher算法:

class Solution(object):
def longestPalindrome(self, s):
t = '$#' + '#'.join(s) + '#_'
return t
p = [0] * 4010
mx, id, mmax, right = 0, 0, 0, 0
for i in range(1, len(t) - 1):
if mx > i:
p[i] = min(p[2 * id - i], mx - i)
else:
p[i] = 1
while t[i + p[i]] == t[i - p[i]]:
p[i] += 1
if i + p[i] > mx:
mx = i + p[i]
id = i
if mmax < p[i]:
mmax = p[i]
right = i
return s[right//2 - mmax//2: right//2 - mmax//2 + mmax - 1]

Longest Palindromic Substring的更多相关文章

  1. 最长回文子串-LeetCode 5 Longest Palindromic Substring

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

  2. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

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

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

  4. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  5. Leetcode Longest Palindromic Substring

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

  6. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  7. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  8. 5. Longest Palindromic Substring

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

  9. leetcode-【中等题】5. Longest Palindromic Substring

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

  10. Leetcode5:Longest Palindromic Substring@Python

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

随机推荐

  1. nokogiri如何使用

    直接来个简单的代码实例就明白啦! require 'nokogiri' xml_data=<<XML <library> <NAME><![CDATA[Fav ...

  2. PHP导出数据到excel的方法

    很简单,看内容,不多说了: <?php //设置标题 $header[] = "ID"; $header[] = "订单"; $header[] = &q ...

  3. Hibernate5.2关联关系之单向一对多(一)

    Hibernate5.2之单向一对多 一. 简介    Hibernate中最复杂的应该就是各种关联(单向一对多.单向多对一.双向一对多.一对一.多对多)关系的映射,于是笔者就想着去写一些关于Hibe ...

  4. (C#)算法题

    1. Convert string from "AAABBCC" to "A3B2C2". 当面试者提出这个问题的时候,首先需要确认题意:譬如:字符串是不是顺序 ...

  5. CSS组件架构的设计思想

    不管是设计思想,还是架构,都可以总结为一个词:AO模式.A表示Append,即“附加”的意思,O表示Overwrite,即“重写”的意思.所有的CSS组件都是沿用这种思想来设计的.这也是CSS的特性, ...

  6. 在 SQL Server 数据库的 WHERE 语句中使用子查询

    这是关于子查询语句的一系列文章中的第三篇.在这篇文章中我们将讨论WHERE语句中的子查询语句.其他的文章讨论了其他语句中的子查询语句. 本次课程中的所有例子都是基于Microsoft SQL Serv ...

  7. 关于sql server远程访问Oracle数据库 OpenQuery查询返回多条数据的问题

    在Sql Server远程访问Oracle 中的数据库表时: 远程语法通常为: select * from OpenQuery(Oracle链接服务器名称,‘查询语句’) eg: select * f ...

  8. (String) 205.Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. WebService到底是什么?

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  10. MC的缓存雪崩现象和缓存无底洞的原因以及导致的后果的总结

    缓存雪崩一般是由某个缓存节点失效,导致其他节点的缓存命中率下降,缓存中确实的数据去数据库查询,短时间内,造成数据库服务器的崩溃. 这时,我们需要重启数据库,但重启一段时间后,又会被压垮,但此时缓存的数 ...