Leetcode 第5题 Longest Palindromic Substring
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:
def longestPalindrome(self, s):
res = ""
for i in range(len(s)):
# 对于奇数子字符串来说
tmp = self.helper(s, i, i)
if len(tmp) > len(res):
res = tmp
# 对于偶数子字符串来说
tmp = self.helper(s, i, i+1)
if len(tmp) > len(res):
res = tmp
return res
# from 从里向外找
def helper(self, s, l, r):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1; r += 1
return s[l+1:r]
这道题难了我一天,
明天我再试着写一下。
英国
03/06/2020