Leetcode5:Longest Palindromic Substring@Python
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example:
Input: "cbbd" Output: "bb" 动态规划法:以"ababae"为例:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmsAAAD0CAIAAABLgfYMAAARXklEQVR4nO2dwZni3BJDOy4CIh6iIRmCqX/RMwMuevPcdXUf0jnL2WjOJ9syBpqvAgAAgP+dr93/AQAAgI+EBQUAADgDCwoAAHAGFhQAAOAMLCgAAMAZWFAAAIAzsKAAAABnYEEBAADOwIICAACcgQUFAAA4AwsKAABwBhYUAADgDCwoAADAGVhQAACAM7CgAAAAZ2BBAQAAzsCCAgAAnIEFBQAAOAMLCgAAcAYWFAAA4AwsKAAAwBlYUAAAgDOwoAAAAGdgQQEAAM7AggIAAJyBBQUAADjDhgX9AgAAWIxizgQZPVIi9v8Apn5gakmOLKbDKYKMHkmFdmDqR45pJcliOpwiyOiRVGgHpn7kmFaSLKbDKYKMHkmFdmDqR45pJcliOpwiyOiRVGgHpn7kmFaSLKbDKYKMHkmFdmDqR45pJcliOpwiyOiRVGiH0vR+/fq63B6yvCN0uoK9nRa1OsKCfjyYroAF1ZDTaVGrIyzox4PpClhQDTmdFrU6woJ+PJiugAXVkNNpUasjLOjHozV93C7PP2d1vQuTt1xt79ctsnS6gr2dFrWu46VTsSoL+vEITR+3y/P4fNwu4lt69dX26+knlqXTFezttKh1DY/b5au5CkeUBf14tpmqj9W9T/ykjwDpdAV7Oy1qXcL92tTe/mEpLOjHs/HRkPGzzbeLq/QiRKcr2NtpUesKuuXX4TnDeljQj0d8u7fveUnQ1ZZOV5C0oDG1ytUaLOjHs+1gNT4tdz/xo9MVBD3FDap18yesWdCPZ9ON7Z+nJ6anZf/UyfGWfjl0uoK9nRa1rqF/Tup+VS4qC/rxSE0P3wTQvmWvv9pe7y9vsth+lJFOZVDrIg5vhmpbZUE/Hkz9wNSSHFl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jmmf/gWNjVlQRciPy3Nfw7+G0w15JiWUDbH9JvnL2yzoGORLOg892v7Cfi3f1gKpgvAVJWvk80xrXreLjxuFxZ0MFK8oM+7IN/bvfdDVHvQYjoPpsKTVSSbY3qABZ2NVN8E/atOXaTO9Ic7WenNLabzYCp8aSaSzTE9wILORmqfDh2Ke/uHpeRcgzCdB1O/XckxPcCCzkZqnw694fhO0u4zE9N5MPXblRzTAyzobOTG91e05LyThOk8mPq9O5hjeoAFnY3c+Bk/LRtNjx+aXw6mC8BUlb/vs7i+pq+woLOR275nVnW/Kg9Xpen9+vqA2vm7g5hqyDEtoWyO6Qss6Gzkxr91on1Buu3Ly/JX3pguAlMN8tuFCNOfPoji9hDFf0E3gqkfmFqSI4vpcIogo0dSoR2Y+pFjWkmymA6nCDJ6JBXagakfOaaVJIvpcIogo0dSoR2Y+pFjWkmymA6nCDJ6JBXagakfOaaVJIvpcIogo0dSoR2Y+pFjWkmymA6nCDJ6JBXagakfOaaVJIvpcIogo0dSoR2Y+pFjWkmymA6nCDJ6JAAAwGIUcybIAAAA8IMFBQAAOAMLCgAAcAYWFAAA4AwsKAAAwBlYUAAAgDOwoAAAAGdgQQEAAM7AggIAAJyBBQUAADgDCwoAAHAGFhQAAOAMLCgAAMAZWFAAAIAzsKAAAABnYEEBAADOwIICAACcgQUFAAA4AwsKAABwBhYUAADgDCwoAADAGVhQAACAM7CgAAAAZ2BBAQAAzsCCAgAAnIEFBQAAOAMLCgAAcAYWFAAA4Az/AZpE1th2lqslAAAAAElFTkSuQmCC" alt="" width="548" height="207" />
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
sLength=len(s)
begin=0
length=0
table=[]
for i in range(sLength):
tem=[]
for j in range(sLength):
tem.append(0)
table.append(tem) for i in range(sLength):
table[i][i]=1 for i in range(sLength-1):
if s[i]==s[i+1]:
table[i][i+1]=1
begin=i
length=1 for tem in range(1,sLength):
for i in range(sLength-tem):
j=i+tem
if (s[i]==s[j] and table[i+1][j-1])==1:
table[i][j]=1
length=tem
begin=i return s[begin:begin+length+1]
Leetcode5:Longest Palindromic Substring@Python的更多相关文章
- LeetCode5:Longest Palindromic Substring
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- lintcode :Longest Palindromic Substring 最长回文子串
题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- 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(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- LeetCode OJ:Longest Palindromic Substring(最长的回文字串)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode第五题:Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 【LeetCode5】Longest Palindromic Substring★★
1.题目描述: 2.解题思路: 题意:求一个字符串的最长回文子串. 方法一:中心扩展法.遍历字符串的每一个字符,如果存在回文子串,那么中心是某一个字符(奇数)或两个字符的空隙(偶数),然后分两种情况( ...
- leetcode Longest Palindromic Substring python
class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str ...
随机推荐
- 1.5 linux笔记
fdisk /dev/sda 查看sda下硬盘状态 fdisk l 看LIST所有文件系统 fdisk n 创建分区 fdisk m help fdisk p 查看所有分区 fdisk w 保存分 ...
- Pyhton的发展历程
Python的由来 在1989年12月时,吉多·范罗苏姆——龟叔,想寻找一门“课余”编程项目来打发圣诞节前后的时间.Guido决定为当时正构思的一个新的脚本语言写一个解释器,它是ABC语言(教学语言. ...
- linux开机启动mongodb
方式一(不推荐) ubuntu编辑/etc/rc.local /home/wyt/bin/mongodb-linux-x86_64-ubuntu1404-3.2.8/bin/mongod --dbpa ...
- 安装恢复Windows10各种坑
1.安装Windows10最好用微软原装ISO,其它的第三方坑比较多,要么不支持UEFI,要么不支持MBR,要么不支持GPT. 2.恢复系统可以使用WIM备份,或Window原生备份,或者Ghost备 ...
- wine
sudo dpkg --add-architecture i386 sudo add-apt-repository ppa:wine/wine-buildssudo apt-get update su ...
- 在Ubuntu上安装LAMP服务器
1.安装Ubuntu上安装LAMP apt-get install lamp-server^ 2.安装过程中设置MySql密码 3.测试 创建index.php var/www/html/index. ...
- (转载)iOS开发历程书籍推荐
http://www.zhihu.com/question/19649679 我来说说我学习的过程:(无耻的使用一个回答 回答了很多问题.^^) 第一步,精读 Stephen G. Kochan 的& ...
- AFNetwork ATS 网络层改造
最近一直做项目的ATS改造,期间遇到了种种问题,各种坑都记录下来, 比如iOS版本.afnetwork版本.证书(是否为自签证书).域名验证.TLS版本等等,我们项目更复杂,还使用了域名到IP映射的路 ...
- <<Vector Calculus>>笔记
现在流行用Exterior Caculus, 所以个人觉得Matthews这本书有点过时了. 想学Vector Calculus的话,推荐<Vector Calculus, Linear Alg ...
- ios打包出来为pkg的处理方法
Add LSRequiresIPhoneOS YES to your Info.plist The key can be found as Application requires iPhone en ...