leetcode解题—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, and there exists one unique longest palindromic substring.
解题:参考网上大神做法,解题如下:
class Solution: def get_palindromic(self, s, k, l): s_len = len(s) while k >= 0 and l < s_len and s[k] == s[l]: k -= 1 l += 1 return s[k+1:l] def longestPalindrome(self, s): L_palindromic = '' for i in range(len(s)): temp_palindromic1 = self.get_palindromic(s, i, i) if len(temp_palindromic1) > len(L_palindromic): L_palindromic = temp_palindromic1 temp_palindromic2 = self.get_palindromic(s, i, i+1) if len(temp_palindromic2) > len(L_palindromic): L_palindromic = temp_palindromic2 return L_palindromic