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 ...
随机推荐
- windows puppet manifests 文件维护
初级 puppet windows agent实现简单的msi格式安装包安装及bat文件创建;
- 使用Python做科学计算初探
今天在搞定Django框架的blog搭建后,尝试一下python的科学计算能力. python的科学计算有三剑客:numpy,scipy,matplotlib. numpy负责数值计算,矩阵操作等: ...
- hdu 4055 Number String(dp)
Problem Description The signature of a permutation is a string that is computed as follows: for each ...
- .NET 面试题(2)
61.Application .Cookie和 Session 两种会话有什么不同? 1.Application 储存在服务端,没有时间限制,服务器关闭即销毁(前提是自己没写销毁方法) 2.Sessi ...
- Django之上传文件
使用Form表单上传文件 upload.html <!DOCTYPE html> <html lang="en"> <head> <met ...
- PHP本地域名解析教程
1.找到C:\WINDOWS\system32\drivers\etc\hosts 127.0.0.1 localhost 127.0.0.1 www.zhosoft.com ...
- java动态代理和cglib动态代理
动态代理应用广泛,Spring,Struts等框架很多功能是通过动态代理,或者进一步封装来实现的. 常见的动态代理模式实现有Java API提供的动态代理和第三方开源类库CGLIB动态代理. Java ...
- 用Spring提高java观察者模式灵活性
在上篇博客 用java观察者模式解耦经典三层架构 的最后,用了一个Client类把Listener的实现类注冊到了LoginEventSource类中,假设须要加入�新的逻辑,加入�新的listene ...
- React数据传递
React基础概念 React是基于组件化的开发,通过组件的组合,让web应用能够实现桌面应用的效果. React更有利于单页应用的开发. 并非MVC框架,只能算是V 具有单项数据流的特点 优势:代码 ...
- hdu 1234
Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签 到.签离记录,请根据记录找出当天开门和关门的人. Input 测试输入的第一行 ...