码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode] 5.Longest Palindromic Substring

时间:2019-04-27 09:39:30      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:leetcode   sub   char   HERE   color   lin   else   limit   operator   

开始觉得挺简单的 写完发现这个时间超限了:

class Solution:
    def longestPalindrome(self, s: str) -> str:
        # longest palindromic substring lenth < 1000
        #NULL
        string = s
        if len(string) == 0:
            return ‘‘
        # ---
        # regular
        # a not empty string must have 1 letterd palindromic substring:string[0]
        longestPalindSubStringLength = 1
        longestPalindSubString = string[0]
        for start in range(len(string)):
            for end in range(start + 1, len(string) +1): #[] operator is right open interval,will not count "end" if no +1
                # one letter
                if len(string[start:end]) == 1:
                    continue
                if (self.isPalind(string[start:end])):
                    if len(string[start:end]) > longestPalindSubStringLength:# > will get bab ;>= will get aba. I prefer simple one
                        longestPalindSubStringLength = len(string[start:end])
                        longestPalindSubString = string[start:end]
        return longestPalindSubString

    def isPalind(self, substring: str) -> bool:
        #notice substring >= 2
        substringlength = len(substring)

        looplimit = substringlength // 2
        for index in range(0, looplimit):
            if substring[index] != substring[substringlength-1 -index]: # axisymmetric
                return False
            else:
                pass
        # run to here without return mean palindromic
        return True
"abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababa"

 

用pycharm跑了下 要4秒,结果就是它自己。[testcase:85]

 

发现之前判断轴对称的方法太低级了 不用逐位判断 轴对称的前一半和后一半的逆序是同一字符串即可。

 

class Solution:
    def longestPalindrome(self, s: str) -> str:
        # longest palindromic substring lenth < 1000
        #NULL
        string = s
        if len(string) == 0:
            return ‘‘
        # ---
        # regular
        # a not empty string must have 1 letterd palindromic substring:string[0]
        longestPalindSubStringLength = 1
        longestPalindSubString = string[0]
        for start in range(len(string)):
            for end in range(start + 1, len(string) +1): #[] operator is right open interval,will not count "end" if no +1
                # one letter
                if len(string[start:end]) == 1:
                    continue
                if (self.isPalind(string[start:end])):
                    if len(string[start:end]) > longestPalindSubStringLength: #> mean bab;>= mean aba
                        longestPalindSubStringLength = len(string[start:end])
                        longestPalindSubString = string[start:end]
        return longestPalindSubString

    def isPalind(self, substring: str) -> bool:
        #notice substring >= 2
        substringlength = len(substring)

        if(substringlength %2)  == 1:
            #odd
            temp1 = substring[0:substringlength // 2 +1]
            temp2 = substring[substringlength:substringlength // 2 - 1:-1]
            if temp1 == temp2:
                return True
            else:
                return False
        else:
            #even
            temp1 = substring[0:substringlength//2]
            temp2 = substring[substringlength:substringlength//2 -1:-1]
            if temp1 == temp2:
                return True
            else:
                return False

 

testcase:95

 

"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"

 

f比g多一个

 

[leetcode] 5.Longest Palindromic Substring

标签:leetcode   sub   char   HERE   color   lin   else   limit   operator   

原文地址:https://www.cnblogs.com/alfredsun/p/10777340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!