欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

leetcode5:Longest Palindromic Substring最长回文子串

程序员文章站 2024-02-24 17:56:10
...

python版:

class Solution:
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        start = end = 0
        for i in range(len(s)): 
            len1 = self.find(s,i,i) #aba
            len2 = self.find(s,i,i+1) #abba
            maxlen = max(len1,len2)
            if maxlen>end-start+1:
                start = i-(maxlen-1)//2
                end = i+maxlen//2
        return s[start:end+1]
           
        
    def find(self,s,left,right):
        while(left>=0 and right<len(s) and s[left]==s[right]):
                left-=1
                right+=1
        return right-left-1

【总结】:
python中的 / 是浮点除法
// 是整除