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. C#命名空间详解namespace

     命名空间是一个域,这在个域中所有的类型名字必须是唯一的,不同的类型分组归入到层次化的命名空间, 命名空间的好处是:1.避免名字冲突,2.便于查找类型名字. 如:System.secruity.Cry ...

  2. web基础-web工作原理,http协议,浏览器缓存

    1,web工作原理 2,http协议 3,浏览器缓存 4,cookie和session -------------------------------------------------------- ...

  3. HTML文本框

    文本框样式大全   输入框景背景透明:<input style="background:transparent;border:1px solid #ffffff"> 鼠 ...

  4. 使用sqlcmd执行连接的时候一直报有语法错误

    1.今天在使用sqlcmd进行执行连接操作的时候一直报有语法错误找了半天. 命令 sqlcmd -S 服务器名称 -U 帐户 -P 密码 示例 sqlcmd -S "LOCALHOST&qu ...

  5. spring加载jar包中多个配置文件

    转自:http://www.cnblogs.com/GarfieldTom/p/3723915.html <import resource="classpath*:applicatio ...

  6. provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接

    问题描述: SQL Sever2012 中:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为 ...

  7. C语言处理CSV文件的方法(一)

    什么是CSV文件 CSV是 Comma-separated values (逗号分隔值)的首字母缩写,它通常是以逗号且不仅限于逗号分隔各个值,我们都叫他CSV. 看下面的例子: China, Shan ...

  8. CentOS yum安装配置lnmp服务器(Nginx+PHP+MySQL)

    1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport  ...

  9. 关于ie6中使用css滤镜[_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/*.png',sizingMethod='scale')]后链接无法点击的问题

    RT,我做的一个效果是试用png图做背景,通过_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/*.png' ...

  10. 解决ie6里png图片透明变白色bug

    加入这段js就行了. function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6. { var a ...