Question

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

Solution

Palindrome类型的题目的关键都是这个递推表达式:

dp[i][j] = (s[i] == s[j]) && dp[i + 1][j - 1]

逆向思维,于是我们想到由 dp[i][j] 可以推出以下:

dp[i - 1][j + 1], dp[i - 2][j + 2], dp[i - 3][j + 3]...

这题的一个思路是用DP构造2D Array,参见 Palindrome Subarray

另一个思路也是借助了DP的思想,时间复杂度仍是O(n2),但是空间复杂度是O(1)

我们对于每个起点遍历,找以 1. 它为中心的最长对称子序列 2. (如果它和它的邻居相等)它和它的邻居为中心的最长对称子序列

代码如下

 class Solution(object):
def spand(self, s, start, end):
length = len(s)
while start >= 0 and end < length:
if s[start] == s[end]:
start -= 1
end += 1
else:
break
return s[start + 1: end] def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
length = len(s)
result = s[0]
for i in range(length - 1):
# sub-length is odd
result1 = self.spand(s, i, i)
if len(result) < len(result1):
result = result1
# sub-length is even
if s[i] == s[i + 1]:
result2 = self.spand(s, i, i + 1)
if len(result) < len(result2):
result = result2
return result

Longest Palindromic Substring 解答的更多相关文章

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

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

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

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

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

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

  4. leetcode--5. Longest Palindromic Substring

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

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

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

  6. No.005:Longest Palindromic Substring

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

  7. Leetcode Longest Palindromic Substring

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

  8. 【leedcode】 Longest Palindromic Substring

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

  9. [LeetCode_5] Longest Palindromic Substring

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

随机推荐

  1. 哲学家用餐问题的几个解法(c语言实现)

    参考资料: 1.维基百科:哲学家用餐问题 2.Windows的多线程编程

  2. html.css随便记

    css 绝对定位:一个元素绝对定位时,浏览器首先将它从流中完全删除,然后浏览器再把这个元素放在属性指定的位置上,对其他元素没有影响   绝对定位要相对于最近的父级元素进行定位 position: ab ...

  3. hdu 3056 病毒侵袭持续中 AC自己主动机

    http://acm.hdu.edu.cn/showproblem.php?pid=3065 刘汝佳的模板真的非常好用,这道题直接过 学到: cnt数组记录单词出现次数 以及map存储单词编号与字符串 ...

  4. Android Fragment详解(六):Fragement示例

    把条目添加到动作栏 你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目,这都需通过实现方法onCreateOptio ...

  5. poj 3009 Curling 2.0 (dfs )

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Desc ...

  6. ARM指令集——条件执行、内存操作指令、跳转指令

    ARM 汇编指令条件执行 在ARM模式下,任何一条数据处理指令可以选择是否根据操作的结果来更新CPSR寄存器中的ALU状态标志位.在数据处理指令中使用S后缀来实现该功能. 不要在CMP,CMN,TST ...

  7. CentOS CVS安装使用

    CentOS CVS安装使用   一.CVS简介 CVS(Concurrent Versions System)版本控制系统:是一种GNU软件包,CVS是一个C/S系统,主要用于在多人开发环境下的源码 ...

  8. JS 图片预览功能

    <script type="text/javascript">    function DisplayImage(fileTag) {        document. ...

  9. 数据库分库分表(sharding)系列(三) 关于使用框架还是自主开发以及sharding实现层面的考量

    当团队对系统业务和数据库进行了细致的梳理,确定了切分方案后,接下来的问题就是如何去实现切分方案了,目前在sharding方面有不少的开源框架和产品可供参考,同时很多团队也会选择自主开发实现,而不管是选 ...

  10. Hadoop基本原理之一:MapReduce

    1.为什么需要Hadoop 目前,一块硬盘容量约为1TB,读取速度约为100M/S,因此完成一块硬盘的读取需时约2.5小时(写入时间更长).若把数据放在同一硬盘上,且全部数据均需要同一个程序进行处理, ...