Skip to content

Commit aad3cd7

Browse files
Merge pull request youngyangyang04#587 from LiangDazhu/patch-32
Update 0005.最长回文子串.md
2 parents 92fcd29 + 811082b commit aad3cd7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

problems/0005.最长回文子串.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,23 @@ public:
270270
## Python
271271

272272
```python
273+
class Solution:
274+
def longestPalindrome(self, s: str) -> str:
275+
dp = [[False] * len(s) for _ in range(len(s))]
276+
maxlenth = 0
277+
left = 0
278+
right = 0
279+
for i in range(len(s) - 1, -1, -1):
280+
for j in range(i, len(s)):
281+
if s[j] == s[i]:
282+
if j - i <= 1 or dp[i + 1][j - 1]:
283+
dp[i][j] = True
284+
if dp[i][j] and j - i + 1 > maxlenth:
285+
maxlenth = j - i + 1
286+
left = i
287+
right = j
288+
return s[left:right + 1]
289+
273290
```
274291

275292
## Go

0 commit comments

Comments
 (0)