class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
lenStr = len(s)
if lenStr <= 0:
return 0
maxLen = 0
tmpLen = 0
tmpRes = ''
for i in range(0,lenStr): #odd numbers
j=0
tmpLen=0
while i-j >= 0 and i+j < lenStr and s[i-j] == s[i+j]:
tmpLen = 2*j +1
j+=1
if tmpLen > maxLen:
maxLen = tmpLen
          #i-j+1 plus one because first: j+=1 then judge s[i-j] == s[i+i]
#so when the while cycle stop, the j is bigger one than the j that make while condition establish
tmpRes = s[i-j+1:i+j]
#even numbers
j=0
tmpLen=0
while i-j>=0 and i+j+1 < lenStr and s[i-j] == s[i+j+1]:
tmpLen = 2*j + 2
j+=1
if tmpLen > maxLen:
maxLen = tmpLen
tmpRes = s[i-j+1:i+j+1]
return tmpRes

leetcode Longest Palindromic Substring python的更多相关文章

  1. [LeetCode] Longest Palindromic Substring 最长回文串

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

  2. Leetcode Longest Palindromic Substring

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

  3. [LeetCode] Longest Palindromic Substring(manacher algorithm)

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

  4. C++ leetcode Longest Palindromic Substring

    明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...

  5. Leetcode: Longest Palindromic Substring && Summary: Palindrome

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

  6. Leetcode5:Longest Palindromic Substring@Python

    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 ...

  8. Leetcode: Longest Palindromic Substring. java

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

  9. LeetCode——Longest Palindromic Substring

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

随机推荐

  1. Web API Login with Cookie

    public HttpWebResponse InitiliazeWebRequest() { string responseData =string.Empty;//string url = Get ...

  2. jQuery源码笔记——延迟对象

    提供一种方法来执行一个或多个对象的回调函数, Deferred对象通常表示异步事件. 它是回调对象的拓展运用,在jQuery当中非常依赖回调对象. 一个简单的,只解决成功状态下的缓存实例 functi ...

  3. T-SQL查询:三值逻辑

    1. 三值逻辑:TRUE / FALSE / UNKNOWN 2. 一个缺失的值(NULL)和另一个值进行比较,逻辑结果是UNKNOWN UNKOWN:NULL > 42 / NULL = NU ...

  4. 关于debug和release 以及new 和delete

    题目:给出一组字符串 输入:"ate","eat","Eat","new","ENW",“wha” ...

  5. Linux程序设计 读笔2 Shell脚本

    第二章 Shell程序设计 四.管道和重定向 1 重定向输出 ls -l > lsoutput.txt ps >> lsoutput.txt >>表示附加到一个文件中 文 ...

  6. Android Fragment StartActivityForresult调用实例

    fragment里面的onActivityResult 怎样才能被调用,很简单,就一句话, startActivityForResult(intent, getActivity().RESULT_FI ...

  7. ajax完成list无刷新返回

    ajax完成list无刷新返回 ajax无刷新技术总结,以下是一段我写的ajax应用的js脚本.其中提交的data刚开始我采用的是$('#formId').serialize();但是出现乱码问题,为 ...

  8. php获取网页内容方法 小偷程序 采集程序

    抓取到的内容在通过正则表达式做一下过滤就得到了你想要的内容,至于如何用正则表达式过滤,在这里就不做介绍了,有兴趣的,以下就是几种常用的用php抓取网页中的内容的方法.1.file_get_conten ...

  9. SQL Server 输出受影响的行

    前期准备: create table Nums(X int); create table T(X int); go 目的:把对表Nums的insert | delete | update 反映到T表中 ...

  10. 数据库合并数据sql

    1.sql2000中只能用自定义的函数解决 )) , 'aa') , 'bb') , 'aaa') , 'bbb') , 'ccc') go )) ) as begin ) select @str = ...