Merge pull request #587 from LiangDazhu/patch-32

Update 0005.最长回文子串.md
This commit is contained in:
程序员Carl
2021-08-11 14:51:14 +08:00
committed by GitHub

View File

@ -270,6 +270,23 @@ public:
## Python
```python
class Solution:
def longestPalindrome(self, s: str) -> str:
dp = [[False] * len(s) for _ in range(len(s))]
maxlenth = 0
left = 0
right = 0
for i in range(len(s) - 1, -1, -1):
for j in range(i, len(s)):
if s[j] == s[i]:
if j - i <= 1 or dp[i + 1][j - 1]:
dp[i][j] = True
if dp[i][j] and j - i + 1 > maxlenth:
maxlenth = j - i + 1
left = i
right = j
return s[left:right + 1]
```
## Go