Merge pull request #180 from QuinnDK/添加0516最长回文子序列Go版本

添加0516最长回文子序列Go版本
This commit is contained in:
Carl Sun
2021-05-19 00:39:47 +08:00
committed by GitHub

View File

@ -173,7 +173,35 @@ Python
Go
```Go
func longestPalindromeSubseq(s string) int {
str:=[]byte(s)
dp:=make([][]int,len(s))
for i:=0;i<len(s);i++{
dp[i]=make([]int,len(s))
}
for i:=1;i<len(s);i++{
for j:=i-1;j>=0;j--{
if str[i]==str[j]{
if j==i-1{
dp[j][i]=2
}else{
dp[j][i]=dp[j+1][i-1]+2
}
}else{
dp[j][i]=Max(dp[j+1][i],dp[j][i-1])
}
}
}
return dp[0][len(s)-1]
}
func Max(a,b int)int{
if a>b{
return a
}
return b
}
```