Longest Palindromic Substring
题目: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的更多相关文章
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- No.005:Longest Palindromic Substring
问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- [LeetCode_5] Longest Palindromic Substring
LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...
- 5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- leetcode-【中等题】5. Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- Leetcode5:Longest Palindromic Substring@Python
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- 一、Spring——IoC
IOC概述 Spring中IOC的概念,控制反转概念其实包含两个层面的意思,"控制"是接口实现类的选择控制权:而"反转"是指这种选择控制权从调用者转移到外部第三 ...
- vim全选,全部复制,全部删除
全选(高亮显示):按esc后,然后ggvG或者ggVG 全部复制:按esc后,然后ggyG 全部删除:按esc后,然后dG 解析: gg:是让光标移到首行,在vim才有效,vi中无效 v : 是进入V ...
- Linux给指定用户或全部用户(已登录)发送消息
在局域网络内很多时候是许多人共用一些机器,但如果多个人同时在使用同一台机器必定会发生一些冲突,比如系统的某些配置被修改,这样引起一些麻烦.那么如果在使用该机器之前,先给登录到该机器的所有其他用户发送一 ...
- [Tex学习笔记]发一篇文章的经历
打算在 INTERNATIONAL JOURNAL OFCONTEMPORARY MATHEMATICAL SCIENCES 发一篇文章, 所以就直接在 作者指引中下载 tex 模版, 写好后发邮件到 ...
- const,static,volatile
1.static 作用 在C语言中,关键字static有三个明显的作用:1). 在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变.2). 在模块内(但在函数体外),一个被声明为静态的 ...
- Android广播机制简介
为什么说Android中的广播机制更加灵活呢?这是因为Android中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容,这些广播可能是来自于系统的,也可能是来 ...
- el 中requestScope和param
${scope.attribute},其中scope指pageSocpe.requestScope.sessionScope.applicationScope,attribute指的就是你在某个sco ...
- (Array)27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- python socket 常见方法及 简单服务/客户端
socket 常见方法: 补充说明:what is file descriptor? 文件描述符是什么? 参考(http://stackoverflow.com/questions/8191905/w ...
- maximo功能修改(初步理解)
已接触IBM公司的MAXIMO近三个月,在这时间里自己对maximo也有所了解,今天将自己总结写在这里,方便自己的温习和大家的参考,不足之处还望指出,我一定在第一时间内修改. 今天在公司所做的就是完善 ...