0005.最长回文子串增加go的dp解法

0005.最长回文子串增加go的dp解法
This commit is contained in:
SwaggyP
2022-08-24 12:41:38 +08:00
committed by GitHub
parent 8e311aeb09
commit 41c480371c

View File

@ -363,6 +363,34 @@ class Solution:
Go Go
```go ```go
func longestPalindrome(s string) string {
maxLen := 0
left := 0
length := 0
dp := make([][]bool, len(s))
for i := 0; i < len(s); i++ {
dp[i] = make([]bool,len(s))
}
for i := len(s)-1; i >= 0; i-- {
for j := i; j < len(s); j++ {
if s[i] == s[j]{
if j-i <= 1{ // 情况一和情况二
length = j-i
dp[i][j]=true
}else if dp[i+1][j-1]{ // 情况三
length = j-i
dp[i][j] = true
}
}
}
if length > maxLen {
maxLen = length
left = i
}
}
return s[left: left+maxLen+1]
}
``` ```