Update 0516.最长回文子序列.md

This commit is contained in:
QuinnDK
2021-05-19 10:07:40 +08:00
committed by GitHub
parent 8c3d8aab25
commit c628620ced

View File

@ -173,6 +173,33 @@ Python
Go
```Go
func longestPalindromeSubseq(s string) int {
lenth:=len(s)
dp:=make([][]int,lenth)
for i:=0;i<lenth;i++{
for j:=0;j<lenth;j++{
if dp[i]==nil{
dp[i]=make([]int,lenth)
}
if i==j{
dp[i][j]=1
}
}
}
for i:=lenth-1;i>=0;i--{
for j:=i+1;j<lenth;j++{
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][lenth-1]
}
```