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

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

View File

@ -175,36 +175,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
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]
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)