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 ...
随机推荐
- overload, override和overwrite之间的区别
Overload.Overwrite和Override的概念比较容易混淆,而且Overwrite和Override的中文翻译五花八门,让人很Confuse,顾保持英文原意: Overload 重载 ...
- ios中strong和weak的解释理解
来自stackoverflow解释的挺有意思的 Imagine our object is a dog, and that the dog wants to run away (be dealloca ...
- DELPHI高性能大容量SOCKET并发(九):稳定性问题解决
http://blog.csdn.net/sqldebug_fan/article/details/9043699
- oracle 利用flashback将备库激活为read wirte(10g 及上)
oracle 利用flashback将备库激活为read wirte(10g 及上) 环境: OS: CENTOS 6.5 X64 DB: ORACLE 10.2.0.5 主库操作: SQL> ...
- Window中常见的dos命令
1.如何实行操作dos命令:如果是Windows电脑,从开始--->所有程序---->附件--->命令提示 这样就可以开始命令提示符了 2关于一些dos命令: 2.1 盘符切换:盘符 ...
- IMX6输出可控PWM
驱动部分 #include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h& ...
- C#中截取字符串的几种方法
1.根据单个分隔字符用split截取 例如 复制代码代码如下: string st="GT123_1"; string[] sArray=st.split("_&qu ...
- Spark Streaming源码解读之生成全生命周期彻底研究与思考
本期内容 : DStream与RDD关系彻底研究 Streaming中RDD的生成彻底研究 问题的提出 : 1. RDD是怎么生成的,依靠什么生成 2.执行时是否与Spark Core上的RDD执行有 ...
- 动态规划 - 最长递增子序列(LIS)
最长递增子序列是动态规划中经典的问题,详细如下: 在一个已知的序列{a1,a2,...,an}中,取出若干数组组成新的序列{ai1,ai2,...,aim},其中下标i1,i2,...,im保持递增, ...
- AbstractQueueSynchronizer
1.AbstractQueuedSynchronizer(以下简称AQS)是Java并发包提供的一个同步基础机制,是并发包中实现Lock和其他同步机制(如:Semaphore.CountDownLat ...