mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #188 from QuinnDK/添加0516最长回文子序列Go版本-1
添加0516最长回文子序列Go版本
This commit is contained in:
@ -175,36 +175,35 @@ Python:
|
|||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
func longestPalindromeSubseq(s string) int {
|
func longestPalindromeSubseq(s string) int {
|
||||||
str:=[]byte(s)
|
lenth:=len(s)
|
||||||
dp:=make([][]int,len(s))
|
dp:=make([][]int,lenth)
|
||||||
for i:=0;i<len(s);i++{
|
for i:=0;i<lenth;i++{
|
||||||
dp[i]=make([]int,len(s))
|
for j:=0;j<lenth;j++{
|
||||||
}
|
if dp[i]==nil{
|
||||||
for i:=1;i<len(s);i++{
|
dp[i]=make([]int,lenth)
|
||||||
for j:=i-1;j>=0;j--{
|
}
|
||||||
if str[i]==str[j]{
|
if i==j{
|
||||||
if j==i-1{
|
dp[i][j]=1
|
||||||
dp[j][i]=2
|
}
|
||||||
}else{
|
}
|
||||||
dp[j][i]=dp[j+1][i-1]+2
|
}
|
||||||
}
|
for i:=lenth-1;i>=0;i--{
|
||||||
}else{
|
for j:=i+1;j<lenth;j++{
|
||||||
dp[j][i]=Max(dp[j+1][i],dp[j][i-1])
|
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][len(s)-1]
|
}
|
||||||
}
|
}
|
||||||
func Max(a,b int)int{
|
}
|
||||||
if a>b{
|
|
||||||
return a
|
return dp[0][lenth-1]
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
Reference in New Issue
Block a user