Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example 2:
Input: “cbbd”
Output: “bb”
回文字符的寻找,两个迭代嵌套,一个从前往后,另一个从后往前,遇到相同的字符就把中间这段字符串取出来,判断是否为回文字符(逆序后不变)。
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if s == '':
return ''
l = len(s)
max = 1
res = s[0]
for i, v1 in enumerate(s):
for j, v2 in enumerate(s[-1:0:-1]):
if s[i:(l-j)] == s[i:(l-j)][::-1]:
temp = l - j - i
if temp > max:
res = s[i:(l-j)]
max = temp
else:
break
return res