From 4a924b62baad79a45121be27318ea04036846df4 Mon Sep 17 00:00:00 2001 From: 20200203127 <846169622@qq.com> Date: Sat, 25 Sep 2021 17:40:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5python=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E5=8F=8C=E6=8C=87=E9=92=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0005.最长回文子串.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index c78b827c..d9ddcc60 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -288,7 +288,34 @@ class Solution: return s[left:right + 1] ``` +> 双指针法: +```python +class Solution: + def longestPalindrome(self, s: str) -> str: + def find_point(i, j, s): + while i >= 0 and j < len(s) and s[i] == s[j]: + i -= 1 + j += 1 + return i + 1, j + + def compare(start, end, left, right): + if right - left > end - start: + return left, right + else: + return start, end + + start = 0 + end = 0 + for i in range(len(s)): + left, right = find_point(i, i, s) + start, end = compare(start, end, left, right) + + left, right = find_point(i, i + 1, s) + start, end = compare(start, end, left, right) + return s[start:end] + +``` ## Go ```go