美文网首页
2020-02-27 Day5 Leetcode: 5. Lon

2020-02-27 Day5 Leetcode: 5. Lon

作者: YueTan | 来源:发表于2020-02-27 09:28 被阅读0次
    class Solution:
        def longestPalindrome(self, s: str) -> str:    
            if len(s)==1:
                return s
            
            long=1
            final_left=-1
            final_right=1
            
            for i in range(1,len(s)-1):
                left=i
                right=i            
                while s[left]==s[right]:                
                    left-=1
                    right+=1 
                    if left<0 or right>len(s)-1:
                        break
                
                if right-left-1>long:
                    long=right-left-1
                    final_left=left
                    final_right=right
            
            
            for i in range(len(s)-1):            
                if s[i]==s[i+1]:
                    left=i
                    right=i+1
                    while s[left]==s[right]:
                        left=left-1
                        right+=1
                        if left<0 or right>len(s)-1:
                            break
                    if right-left-1>long:
                        long=right-left-1
                        final_left=left
                        final_right=right            
            
            return s[final_left+1:final_right]
                    
            
                    
    
                    
            
    

    相关文章

      网友评论

          本文标题:2020-02-27 Day5 Leetcode: 5. Lon

          本文链接:https://www.haomeiwen.com/subject/dqozchtx.html