Longest Palindromic Substring 解答
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 解答的更多相关文章
- 【翻译】Longest Palindromic Substring 最长回文子串
原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 最长回文子串-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 ...
随机推荐
- 第34讲 UI组件之 ProgressDialog和Message
第34讲UI组件之 ProgressDialog和Message 1.进度对话框 ProgressDialog <1>简介 ProgressDialog是AlertDialog类的一个扩展 ...
- (转)iOS7界面设计规范(8) - UI基础 - 术语和措辞
讨厌周一,讨厌一周.今天中午交互组聚餐,却很开心:大家都是很厉害的人,你可以感到他们身上的能量,可以感到有些什么东西正在推着自己尽力向前走.这是一种很健康的状态,同时也很难得,自然越发需要珍惜.从无到 ...
- jps命令使用
jps工具 jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前全部java进程pid的命令,简单有用,很适合在linux/un ...
- 2013 长沙网络赛 B 题 Bizarre Routine
题解 http://blog.csdn.net/u010257508/article/details/11936129 #include <iostream> #include <c ...
- javaweb中去除某个get方式的参数,并且返回路径
String requestURL = request.getRequestURL() + ""; // String queryString = request.getQuery ...
- android 新浪微博客户端的表情功能的实现
这是一篇好文章,我转来收藏,技术的最高境界是分享. 最近在搞android 新浪微博客户端,有一些心得分享弄android客户端表情功能可以用以下思路1.首页把新浪的表情下载到本地一文件夹种,表情图片 ...
- 解决Fetching android sdk component information加载过久问题
安装完成后,如果直接启动,Android Studio会去获取 android sdk 组件信息,这个过程相当慢,还经常加载失败,导致Android Studio启动不起开.解决办法就是不去获取and ...
- DotNet程序汉化过程--SnippetCompiler奇葩的字符串
开篇前言 汉化的过程总会遇到各种各样的问题,让人抓狂,这一篇我就来讲解一下一个特殊的单词的汉化以及我的“艰辛历程”. 起因介绍 在SnippetCompiler有这么一个奇葩的字符串“查找>&g ...
- Android开发环境的搭建之(一)Java开发环境的安装
(1) 安装JDK(Java Developer Kit).下载JDK1.8并安装jdk-8u60-windows-i586.exe.下载官方链接http://www.oracle.com/tech ...
- First 5 minutes of SQLite
What is SQLite? SQLite is light-weight RDBMS, it is use the file system rather than the C/S client, ...