Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41c4803

Browse files
0005.最长回文子串增加go的dp解法
0005.最长回文子串增加go的dp解法
1 parent 8e311ae commit 41c4803

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎problems/0005.最长回文子串.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,34 @@ class Solution:
363363
Go:
364364

365365
```go
366+
func longestPalindrome(s string) string {
367+
maxLen := 0
368+
left := 0
369+
length := 0
370+
dp := make([][]bool, len(s))
371+
for i := 0; i < len(s); i++ {
372+
dp[i] = make([]bool,len(s))
373+
}
374+
for i := len(s)-1; i >= 0; i-- {
375+
for j := i; j < len(s); j++ {
376+
if s[i] == s[j]{
377+
if j-i <= 1{ // 情况一和情况二
378+
length = j-i
379+
dp[i][j]=true
380+
}else if dp[i+1][j-1]{ // 情况三
381+
length = j-i
382+
dp[i][j] = true
383+
}
384+
}
385+
}
386+
if length > maxLen {
387+
maxLen = length
388+
left = i
389+
}
390+
}
391+
return s[left: left+maxLen+1]
392+
}
393+
366394

367395
```
368396

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /