[LeetCode] 5. Longest Palindromic S
問題描述
給定一個字串 s,找出其中最長的迴文子字串,假設 s 的最大長度為 1000。
例 1:
輸入: "babad"
輸出: "bab"
注意: "aba" 也是正確的答案
例 2:
Input: "cbbd"
Output: "bb"
問題難度
Medium
解題思路
注意到迴文的對稱性特點,我們只需要在遍歷 s 的過程中,假設每一個字元都是迴文的中心,對於每一個迴文中心,我們不斷向兩邊擴充套件,同時檢測其對稱性,找出該回文的邊界,並記錄其長度,最終,當遍歷完 s 之後,我們便檢測了 s 中所有的迴文,當然就可以得到 s 中最長的迴文子字串。這種方法的時間複雜度為 KaTeX parse error: Expected 'EOF', got '' at position 7: O(n^2)̲ 。
需要注意的是,迴文有兩種形式:單中心和雙中心,所以我們在遍歷每個字元時,不僅要把當前字元當做單中心迴文的中心,還要將當前字元和下一個字元當做雙中心迴文的中心,並分別以這兩個中心向兩邊擴充套件。
全部程式碼如下:
class Solution():
def expand(self, left, right, s):
"""
expand from middle point
"""
if right >= len(s) or s[left] != s[right]:
return 0
while left-1 >= 0 and right+1 < len(s) and s[left-1] == s[right+1]:
left -= 1
right += 1
return right + 1 - left
def longest_palindrome(self, s):
"""
:type s: str
:rtype: str
"""
if not s:
return ""
middle = 0
max_len = 0
for i in range(len(s)):
len1 = self.expand(i, i, s)
len2 = self.expand(i, i+1, s)
longer = max(len1, len2)
if longer > max_len:
max_len = longer
middle = i
begin = middle-int((max_len-1)/2)
return s[begin:begin+max_len]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2508/viewspace-2823033/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- Leetcode5: Longest Palindromic Substring(最長迴文子串)LeetCode
- Leetcode 32 Longest Valid ParenthesesLeetCode
- Leetcode 14 Longest Common PrefixLeetCode
- [LeetCode] 32. Longest Valid ParenthesesLeetCode
- [LeetCode] 2419. Longest Subarray With Maximum Bitwise ANDLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- [LeetCode] 674. Longest Continuous Increasing SubsequenceLeetCode
- leetcode388. Longest Absolute File PathLeetCode
- Leetcode 298 Binary Tree Longest Consecutive SequenceLeetCode
- Leetcode 329. Longest Increasing Path in a MatrixLeetCode
- [LeetCode] 3239. Minimum Number of Flips to Make Binary Grid Palindromic ILeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- LeetCode Longest Common Prefix(014)解法總結LeetCode
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- [LeetCode] 524. Longest Word in Dictionary through DeletingLeetCode
- leetcode學習筆記14 Longest Common PrefixLeetCode筆記
- C# 寫 LeetCode easy #14 Longest Common PrefixC#LeetCode
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- LeetCode Longest Substring Without Repeating Characters(003)解法總結LeetCode
- LeetCode - 014 - 最長公共字首(longest-common-prefix)LeetCode
- Leetcode 329. Longest Increasing Path in a Matrix (python+cpp)LeetCodePython
- [LeetCode] 3. Longest Substring Without Repeating Characters 題解LeetCode
- LeetCode 5.最長迴文子串LeetCode
- Leetcode[字串] 5. 最長迴文子串LeetCode字串
- LeetCode 5.最長的迴文字串LeetCode字串
- LeetCode | 14. Longest Common Prefix的三種演算法LeetCode演算法
- 【Leetcode】3. Longest Substring Without RepeatingCharacters無重最長子串LeetCodeGC
- D. Non-Palindromic Substring
- [AtCoder Beginner Contest 363] D - Palindromic Number
- Longest Valid Parentheses
- Longest Univalue Path
- 【leetcode】32. Longest Valid Parentheses 最長的有效匹配括號子串長度LeetCode
- LeetCode3:Longest Substring Without Repeating Characters(無重複字元的最長子串)LeetCode字元
- 687-Longest Univalue Path
- 673. Number of Longest Increasing Subsequence
- #3 Longest Substring Without Repeating Characters[M]