diff --git "a/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/Document.md" "b/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/Document.md" new file mode 100644 index 0000000..c52d719 --- /dev/null +++ "b/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/Document.md" @@ -0,0 +1,15 @@ +## 题目 +给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 +**Example1:** +``` +输入: "babad" +输出: "bab" +注意: "aba" 也是一个有效答案。 +``` + +**Example2:** +``` +输入: "cbbd" +输出: "bb" +``` + diff --git "a/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/code.py" "b/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/code.py" new file mode 100644 index 0000000..721a4dc --- /dev/null +++ "b/5. \346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/code.py" @@ -0,0 +1,35 @@ +# class Solution: +# def longestPalindrome(self, s: str) -> str: +# maxstr = '' +# flag = 0 +# for i in range(len(s), 0, -1): +# if flag == 1: +# break +# for j in range(0, len(s)-i+1): +# substr = s[j: j+i] +# if substr == substr[::-1]: +# flag = 1 +# maxstr = substr +# return maxstr + +class Solution: + def longestPalindrome(self, s: str) -> str: + flag = 0 + maxstr = '' + for i in range(len(s), 0, -1): + if flag == 1: + break + for j in range(0, len(s)-i+1): + substr = s[j: j+i] + if substr[0: i//2] == substr[i-i//2:][::-1]: + flag = 1 + maxstr = substr + return maxstr + + +if __name__ == "__main__": + solution = Solution() + string = 'babad' + result = solution.longestPalindrome(string) + + print(result)