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. git术语解释staging,index,cache

    当我在使用git的时候,有三个东西的出现,一度让我非常困扰,就如题所述,staging,index,和cache. 比如,当我阅读git官网提供的电子书<Pro Git>的时候,最初一章里 ...

  2. html p标签换行问题

    /*p标签自动换行*/ p{ word-wrap:break-word; word-break:normal; } /*p强制不换行*/ p{ white-space:nowrap; } /*块级元素 ...

  3. Bubble Sort 冒泡排序

    //Bubble Sort ( O(n²)) public class TestBubbleSort { public int[] bubbleSortArray(int[] arr){ ; i &l ...

  4. poj 2456 Aggressive cows(二分搜索之最大化最小值)

    Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...

  5. UESTC 1811 Hero Saving Princess

    九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/11104265 题目链接 :http://222.197.181.5/proble ...

  6. TTB 基本

    中文名 ,线程构建模块 外文名 Thread Building Blocks 缩    写 TBB 开    发 intel 目录 1线程构建模块 2黑体亮温 3斜交载重轮胎 4串联球轴承     1 ...

  7. IOS 原生解析JSON 问题

    服务器----WebService 返回的是JSON数据 IOS解析报错: Error Domain=NSCocoaErrorDomain Code=3840 "Unable to conv ...

  8. compass安装

    修改ruby软件包的sources 国外服务器不给力,经常链接失败,换成国内淘宝的:https://ruby.taobao.org/ 先移除本有的sources gem sources --remov ...

  9. DevExpress ASPxHtmlEditor控件格式化并导出Word (修复中文字体导出丢失)

    在前台页面中先插入一个ASPxHtmlEditor控件,名为ASPxHtmlEditor1. 我用的Dev版本为14.1 格式化文本 在后台插入如下代码  1     const string css ...

  10. C#随机生成连续多少个十六进制数字

    1.调用系统函数生成全球唯一标识 Guid.NewGuid().ToString(); 2.生成16组十六进制数 ,)+Guid.NewGuid().ToString().Substring(,)+G ...