题目: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. Eclipse *下载

    简单了解,Eclipse是绿色软件,下载下来是个压缩包,只需要解压,加上jdk就可以运行了. 相比MyEclipse而言,它是免费的,后者是收费的.各有侧重吧 有很多人用Eclipse,也有很多人用M ...

  2. List和Map之间的转换和关联

    首先,Map.values返回的是此Map中包含的所有值的collection视图. 然后利用ArrayList的构造器ArrayList(Collection<? extends E> ...

  3. WeX5学习笔记-创建本地APP相关问题

    1.在Native新建[创建本地APP]时, "服务地址”为本地IP和端口号,例如本地IP为192.168.253.1,端口号为8080,则设置为http://192.168.253.1:8 ...

  4. ios uiwebview 上几个技巧

    以下内容转自 CrespoXiao的微博 分享个tips,阅读类app内容加载一般是本地存几个html框架,接口传body过来就行了,目的是节省流量.但是loadHTMLString在内容多尤其是图片 ...

  5. 升级NC6.3

    2014-04-23 江苏建工&用友公司会谈提纲 1,合同规定江苏建工用友NC在实施成功之后三年免服务费(2010年增补了资金管理,如果以2010年作为软件最终实施完成,那么2010-2013 ...

  6. SQL Server 2008 数据库镜像部署实例之二 配置镜像,实施手动故障转移

    SQL Server 2008 数据库镜像部署实例之二 配置镜像,实施手动故障转移 上一篇文章已经为配置镜像数据库做好了准备,接下来就要进入真正的配置阶段 一.在镜像数据库服务器上设置安全性并启动数据 ...

  7. mysql按照中文名称排序

    mysql按照中文名称排序   Sql代码  www.2cto.com   /*   Navicat MySQL Data Transfer      Source Server         : ...

  8. Redis部分数据结构方法小结

    package com.practice.util; import java.util.HashMap; import java.util.List; import java.util.Map; im ...

  9. Spring Boot 性能优化

    spring 框架给企业软件开发者提供了常见问题的通用解决方案,包括那些在未来开发中没有意识到的问题.但是,它构建的 J2EE 项目变得越来越臃肿,逐渐被 Spring Boot 所替代.Spring ...

  10. Raising Modulo Numbers

    Description People are different. Some secretly read magazines full of interesting girls' pictures, ...