5. Longest Palindromic Substring Leetcode Python 2016 new Season
2016-01-04 10:24
633 查看
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest
palindromic substring.
Show Company Tags
Show Tags
Show Similar Problems
There are two cases we need to deal with odd like 'ada' even like 'adda'
class Solution(object):
def getPalindromeString(self, s, left_index, right_index):
while left_index >= 0 and right_index < len(s) and s[left_index] == s[right_index]:
left_index -= 1
right_index += 1
return s[left_index + 1 : right_index]
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
longest_string = ''
for index in range(len(s)):
first_string = self.getPalindromeString(s, index, index)
second_string = self.getPalindromeString(s, index, index + 1)
if len(first_string) > len(longest_string):
longest_string = first_string
if len(second_string) > len(longest_string):
longest_string = second_string
return longest_string
palindromic substring.
Show Company Tags
Show Tags
Show Similar Problems
There are two cases we need to deal with odd like 'ada' even like 'adda'
class Solution(object):
def getPalindromeString(self, s, left_index, right_index):
while left_index >= 0 and right_index < len(s) and s[left_index] == s[right_index]:
left_index -= 1
right_index += 1
return s[left_index + 1 : right_index]
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
longest_string = ''
for index in range(len(s)):
first_string = self.getPalindromeString(s, index, index)
second_string = self.getPalindromeString(s, index, index + 1)
if len(first_string) > len(longest_string):
longest_string = first_string
if len(second_string) > len(longest_string):
longest_string = second_string
return longest_string
相关文章推荐
- Python-OpenCV 处理图像(六):对象识别
- Python-OpenCV 处理视频(五): 运动方向判断
- Python-OpenCV 处理视频(四): 运动检测
- urllib 详解
- Python--基础程序设计
- Python-OpenCV 处理视频(三): 标记运动轨迹
- Numpy:常用函数
- Python--初识
- Python-OpenCV 处理视频(二): 视频处理
- Python-OpenCV 处理视频(一): 输入输出
- Ubuntu下安装Pillow
- python 的encode和decode
- python性能优化
- python实现网站的模拟登录
- python --字符串格式化
- python 对ACCESS数据库操作封装模块
- python实现网站的模拟登录
- Python实现控制台进度条功能
- Python字符串特性及常用字符串方法的简单笔记
- 如何系统地自学一门Python 语言