mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Merge pull request #180 from QuinnDK/添加0516最长回文子序列Go版本
添加0516最长回文子序列Go版本
This commit is contained in:
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user