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. SAE Python使用经验 好文推荐

    SAE Python使用经验 好文推荐 SAE Python使用经验 好文推荐

  2. 小米路由器mini搭建个人静态网站的方法

    小米路由和小米路由mini从本质上来说差距就在1T的硬盘上,其它并没有明显差别,但是功能却差很多,例如:小米路由有自带的LAMP模式,而小米路由mini则没有,换句话说,其实这个功能是被阉割了,仔细研 ...

  3. (转)iOS Wow体验 - 第四章 - 为应用的上下文环境而设计

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第四章译文精选,其余章节将陆续放出.上一篇:Wow ...

  4. 仿糯米弹框效果demo

    代码例如以下: <!doctype html> <html lang="en"> <head> <meta charset="U ...

  5. Javascript基础引用类型之Object

    虽然说ECMAScript也是一门对象语言,但是它和其他面向对象语言还是有区别的,它不具有类和接口等基本结构.所以在ECMAScript中一般说类指的是引用类型.创建Object实例的方式有两种: 第 ...

  6. CSS3绘制环形进度条

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. Windows消息机制(转)1

    Windows的应用程序一般包含窗口(Window),它主要为用户提供一种可视化的交互方式,窗口是总是在某个线程(Thread)内创建的.Windows系统通过消息机制来管理交互,消息(Message ...

  8. 作为java应届生,面试求职那点事

    找工作两星期多了.心情不爽,写点记录打发时间. 嘘~~自己的破事:  刚毕业,也过了实习,本理所应当的留在公司转正.可是为了谈了两年的女朋友回家见面.一切都顺利进行,妈妈也开心给了一万见面礼,一切都以 ...

  9. poj2709 贪心基础

    D - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...

  10. MYSQL常用简单语句

    使用SQL语法大写,增加可读性(小写部分就是自己数据库写的表/字段喽,具体你懂得...). 创建数据库:CREATE DATABASE mysql_db;删除数据库:DROP DATABASE mys ...