mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Update 0516.最长回文子序列.md
This commit is contained in:
@ -170,7 +170,20 @@ public class Solution {
|
||||
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def longestPalindromeSubseq(self, s: str) -> int:
|
||||
dp = [[0] * len(s) for _ in range(len(s))]
|
||||
for i in range(len(s)):
|
||||
dp[i][i] = 1
|
||||
for i in range(len(s)-1, -1, -1):
|
||||
for j in range(i+1, len(s)):
|
||||
if s[i] == s[j]:
|
||||
dp[i][j] = dp[i+1][j-1] + 2
|
||||
else:
|
||||
dp[i][j] = max(dp[i+1][j], dp[i][j-1])
|
||||
return dp[0][-1]
|
||||
```
|
||||
|
||||
Go:
|
||||
```Go
|
||||
|
Reference in New Issue
Block a user