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/Hkz9wNSSHFlMh1MEGT2SCu3A1I8c00q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fw7+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的更多相关文章

  1. LeetCode5:Longest Palindromic Substring

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  2. lintcode :Longest Palindromic Substring 最长回文子串

    题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...

  3. LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

    题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...

  4. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  5. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  6. LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. LeetCode第五题:Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  8. 【LeetCode5】Longest Palindromic Substring★★

    1.题目描述: 2.解题思路: 题意:求一个字符串的最长回文子串. 方法一:中心扩展法.遍历字符串的每一个字符,如果存在回文子串,那么中心是某一个字符(奇数)或两个字符的空隙(偶数),然后分两种情况( ...

  9. leetcode Longest Palindromic Substring python

    class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str ...

随机推荐

  1. Nginx-Lua模块的执行顺序

    一.nginx执行步骤 nginx在处理每一个用户请求时,都是按照若干个不同的阶段依次处理的,与配置文件上的顺序没有关系,详细内容可以阅读<深入理解nginx:模块开发与架构解析>这本书, ...

  2. 怎么使用CDR中排列对象功能

    通过将对象发送到其他对象的前面或者后面,可以更改图层或页面上对象的堆叠顺序,还可以将对象按堆叠顺序精确定位,并且可以反转多个对象的堆叠顺序.本教程将详解CorelDRAW中排列对象各按钮功能. 1. ...

  3. 如何给Visual Studio 2015安装XNA4.0

    从这个地址下载安装包 https://mxa.codeplex.com/releases/view/618279 按照说明步骤依次安装 DirectX XNA Framework 4.0 Redist ...

  4. vector与list区别

    vector为存储的对象分配一块连续的地址空间,因此对vector中的元素随机访问效率很高.在vecotor中插入或者删除某个元素,需要将现有元素进行复制,移动.如果vector中存储的对象很大,或者 ...

  5. IOS开发-影院选座算法 限制产生孤座

    众所周知目前影院选座是不允许随便选的,我们不可以任性的挑三拣四,最后留下N个单独的座位,目的就是要留下至少2个连着的座位: 另外有些影院的座位摆放并不是规则的,有些座位被过道或者特殊座位分割开,产生了 ...

  6. nodejs+sequelize操作mysql数据库

    前言: 本人对mysql不是很熟悉,只会命令行的简单增删改查.有些观点可能不到位请谅解. sequelize是针对node.js和io.js开发的基于ORM的框架,它支持的数据库包括:PostgreS ...

  7. ngCordova

    参见:http://blog.csdn.net/Luo_xinran/article/details/52164480 ngCordova是基于Cordova封装的AngularJS的调用本地设备接口 ...

  8. ruby中excel简单操作以及文件读取操作方法

    # -*-coding:utf-8 -*-#author:kanlijunrequire 'win32ole'require 'fileutils'class ResultAnalyse @@i=0 ...

  9. 真机调试时遇到“Could not launch *** process launch failed: Security”的解决办法

    半年没写ios程序了,打算重新将这块技术捡回来.谁知道写的第一个测试程序在真机上就跑出来因为安全问题不能加载的情况. ios的版本是9.2的.看提示信息是app的启动被ios的安全机制阻挡了. 在手机 ...

  10. 使用git新建分支以及管理分支

    在进行分支相关的操作前, 我们需要保持主分支干净, 所谓的干净就是没有任何改变(所有更改都已经commit 并 push),那么你可以在任何时候从你的主分支创建一个新分支. 为了方便代码管理,我们应该 ...