Login light

time:2019-12-11 14:57:00

class Solution:
    def longestPalindrome(self, s: str) -> str:
        ret = ''
        #最长子串的长度
        for maxLength in range(1,len(s)+1)[::-1]:
            #判断该长度的子串是否存在
            for i in range(len(s)):
                stmp = s[i:i+maxLength]
                #长度不够,当前长度不符合要求
                if len(stmp) != maxLength:
                    break
                else:
                    if stmp == stmp[::-1]:
                        ret = stmp
                    else:
                        continue
            if ret != '':
                break
        return ret

time:2020-05-21 18:10:00

class Solution {

    /**
     * @param String $s
     * @return String
     */
    function longestPalindrome($s) {
        $maxSubStr = '';
        $maxLen = 0;
        $sLen = strlen($s);
        for($i = 0; $i < $sLen; $i++){
            for($o = $i + $maxLen; $o < $sLen; $o++){
                if($s[$i] != $s[$o]){
                    continue;
                }else{
                    $substr = substr($s, $i, $o - $i + 1);
                    if($substr == strrev($substr)){
                        $maxSubStr = $substr;
                        $maxLen = strlen($substr);
                    }else{
                        continue;
                    }
                }
            }
        }
        return $maxSubStr;
    }
}