leetcode Longest Palindromic Substring python
2015-11-08 20:48
731 查看
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
相关文章推荐
- Python输入函数:raw_input() 与 input() 的区别
- Python应用与实践
- 支持向量机 SVM 算法推导优缺点 代码实现 in Python
- Python之旅(一)
- Python 运算符
- Python学习笔记(5),Battleship 游戏
- Python_强制数据类型转换
- Python: Soft_max 分类器
- python之模块colorsys颜色转换模块 暂不了解
- python爬虫工具及最佳实践
- vim 创建Python脚本时候自动补全解释器和编码方法
- Python初始基本数据类型
- python之模块cmath
- python之模块chunk,了解即可
- 基于python2【重要】怎么自行搭建简单的web服务器
- 业余时间没事做,可以试试这些......
- Python学习笔记
- python学习记——爬糗事百科
- NumPy-快速处理数据--ufunc运算--广播--ufunc方法
- 《用Python玩转数据》第1周学习笔记(Part 1)