leetcode Longest Palindromic Substring python
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的更多相关文章
- [LeetCode] 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 ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- Leetcode: Longest Palindromic Substring && Summary: Palindrome
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- Leetcode5:Longest Palindromic Substring@Python
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 ...
- Leetcode: Longest Palindromic Substring. java
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 ...
随机推荐
- HDU 1134 卡特兰数 大数乘法除法
Problem Description This is a small but ancient game. You are supposed to write down the numbers 1, ...
- devexpress中用ChartControl生成柱状图
在界面中拖入一个ChartControl控件,然后添加一个simplebutton控件.在simplebutton控件的click事件中加入如下代码: private void button1_Cli ...
- wcf系列学习5天速成——第四天 wcf之分布式架构
今天是wcf系列的第四天,也该出手压轴戏了.嗯,现在的大型架构,都是神马的, nginx鸡群,iis鸡群,wcf鸡群,DB鸡群,由一个人作战变成了群殴....... 今天我就分享下wcf鸡群,高性能架 ...
- 获取xml文件
<?xml version="1.0" encoding="utf-8" ?><ArrayOfSystemRool xmlns:xsi=&qu ...
- 浅谈C中的指针和数组(七)
现在到揭露数组名本质的时候了,先给出三个结论: (1)数组名的内涵在于其指代实体是一种数据结构,这种数据结构就是数组: (2)数组名的外延在于其可以转换为指向其指代实体的指针,而且是一个指针常量: ( ...
- ASP.NET MVC3使用Unity2.0实现依赖注入(转载和扩展)
http://note.youdao.com/share/?id=53252d0f897e0e109aadd296a1682354&type=note
- 【Chromium中文文档】Chromium如何展示网页
Chromium如何展示网页 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//Start_Here_Backgrou ...
- Bash debug
Debugging bash scripts Bash can help us to find problems in bash scripts in some ways. You don't exp ...
- C++死锁解决心得
一. 概述C++多线程开发中,容易出现死锁导致程序挂起的现象.关于死锁的信息,见百度百科http://baike.baidu.com/view/121723.htm. 解决步骤分为三步:1.检测死锁线 ...
- 2016 Multi-University Training Contest 7 总结
第七场多校的排名稍微有了一点回升,然而也并不太乐观. 开场欣君秒出了02题的公式,磊哥开始打表验证,发现可行,一A. 我觉得06题有些思路,开始写,但是发现复杂度优化不下去,于是弃疗. 磊哥做了10题 ...