您的位置:首页 > 编程语言 > Python开发

leetcode python - Longest Palindromic Substring

2017-04-05 14:36 330 查看
# Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
#
# Example:
#
# Input: "babad"
#
# Output: "bab"
#
# Note: "aba" is also a valid answer.
# Example:
#
# Input: "cbbd"
#
# Output: "bb"
class Solution(object):

def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
res = ''
curLength = 0
for i in xrange(len(s)):
if self.isPalindrome(s, i - curLength - 1, i):
res = s[i - curLength - 1 : i + 1]
curLength += 2
elif self.isPalindrome(s, i - curLength, i):
res = s[i - curLength : i + 1]
curLength += 1
return res

def isPalindrome(self, s, begin, end):
if begin < 0:
return False
while begin < end:
if s[begin] != s[end]:
return False
begin += 1
end -= 1
return True
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: