Skip to content

Commit ad7e095

Browse files
committed
feat: 0005. Longest Palindromic Substring
1 parent 943a9ea commit ad7e095

File tree

5 files changed

+163
-18
lines changed

5 files changed

+163
-18
lines changed
Loading

Leetcode/0005.Longest-Palindromic-Substring/README.md

+96-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
---
2-
title: 0005.Longest Palindromic Substring
3-
tags: Medium, Dynamic Programming
4-
author: Kimi Tsai <[email protected]>
5-
description:
2+
title: 0005. Longest Palindromic Substring
3+
subtitle: "https://leetcode.com/problems/remove-nth-node-from-end-of-list/"
4+
date: 2024-03-23T21:08:00+08:00
5+
lastmod: 2024-03-23T21:08:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0005. Longest Palindromic Substring"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Medium, DP, Amazon, Microsoft, Google, Adobe, Facebook]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
660
---
61+
762
# [0005.Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)
863

964
## 題目
@@ -34,9 +89,46 @@ Constraints:
3489
給你一個字符串 s,找到 s 中最長的回文子串。
3590

3691
## 解題思路
92+
* 每一個字符本身都是回文
93+
* 長度為 2, 且首尾字符相同則為回文
94+
* 長度>=3, 如果頭尾相同, 則去掉頭尾後可看是合是回文. 如果頭尾不同則不是回文
3795

96+
![](0005.LongestPalindromicString.jpg)
3897

3998
## 來源
4099
* https://leetcode.com/problems/longest-palindromic-substring/
100+
* https://leetcode.cn/problems/longest-palindromic-substring/solutions/1348874/s-by-xext-5zk3/
41101

42102
## 解答
103+
```go
104+
package longestpalindromicsubstring
105+
106+
func longestPalindrome(s string) string {
107+
dp := make([][]bool, len(s))
108+
result := s[0:1]
109+
110+
for i := 0; i < len(s); i++ {
111+
dp[i] = make([]bool, len(s))
112+
dp[i][i] = true // 每個字符本身都是回文
113+
}
114+
for length := 2; length <= len(s); length++ {
115+
for start := 0; start < len(s)-length+1; start++ {
116+
end := start + length - 1
117+
if s[start] != s[end] {
118+
// 字頭字尾不同, 不是回文
119+
continue
120+
} else if length < 3 {
121+
// 長度為2且字頭字尾相同, 則為回文
122+
dp[start][end] = true
123+
} else {
124+
// 狀態轉移 : 去掉字頭字尾, 判斷是否還是回文
125+
dp[start][end] = dp[start+1][end-1]
126+
}
127+
if dp[start][end] && (end-start+1) > len(result) {
128+
result = s[start : end+1]
129+
}
130+
}
131+
}
132+
return result
133+
}
134+
```
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
package longestpalindromicsubstring
22

3-
func LongestPalindromeSubstring(s string) string {
3+
func longestPalindrome(s string) string {
4+
dp := make([][]bool, len(s))
5+
result := s[0:1]
46

5-
return ""
6-
}
7-
8-
type numbers interface {
9-
int | int8 | int16 | int32 | int64 | float32 | float64
10-
}
11-
12-
func max[T numbers](a T, b T) T {
13-
if a > b {
14-
return a
7+
for i := 0; i < len(s); i++ {
8+
dp[i] = make([]bool, len(s))
9+
dp[i][i] = true // 每個字符本身都是回文
10+
}
11+
for length := 2; length <= len(s); length++ {
12+
for start := 0; start < len(s)-length+1; start++ {
13+
end := start + length - 1
14+
if s[start] != s[end] {
15+
// 字頭字尾不同, 不是回文
16+
continue
17+
} else if length < 3 {
18+
// 長度為2且字頭字尾相同, 則為回文
19+
dp[start][end] = true
20+
} else {
21+
// 狀態轉移 : 去掉字頭字尾, 判斷是否還是回文
22+
dp[start][end] = dp[start+1][end-1]
23+
}
24+
if dp[start][end] && (end-start+1) > len(result) {
25+
result = s[start : end+1]
26+
}
27+
}
1528
}
16-
return b
29+
return result
1730
}

Leetcode/0005.Longest-Palindromic-Substring/main_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var tests = []struct {
1818

1919
func TestLongestPalindromeSubstring(t *testing.T) {
2020
for _, tt := range tests {
21-
if got := LongestPalindromeSubstring(tt.arg1); got != tt.want {
21+
if got := longestPalindrome(tt.arg1); got != tt.want {
2222
t.Errorf("got = %v, want = %v", got, tt.want)
2323
}
2424
}
@@ -27,7 +27,7 @@ func TestLongestPalindromeSubstring(t *testing.T) {
2727
func BenchmarkLongestPalindromeSubstring(b *testing.B) {
2828
b.ResetTimer()
2929
for i := 0; i < b.N; i++ {
30-
LongestPalindromeSubstring(tests[0].arg1)
30+
longestPalindrome(tests[0].arg1)
3131
}
3232
}
3333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 時間複雜度:
2+
// 空間複雜度:
3+
/*
4+
* @lc app=leetcode.cn id=5 lang=golang
5+
*
6+
* [5] 最长回文子串
7+
*/
8+
9+
// @lc code=start
10+
func longestPalindrome(s string) string {
11+
dp := make([][]bool, len(s))
12+
result := s[0:1]
13+
14+
for i := 0; i < len(s); i++ {
15+
dp[i] = make([]bool, len(s))
16+
dp[i][i] = true // 每個字符本身都是回文
17+
}
18+
for length := 2; length <= len(s); length++ {
19+
for start := 0; start < len(s)-length+1; start++ {
20+
end := start + length - 1
21+
if s[start] != s[end] {
22+
// 字頭字尾不同, 不是回文
23+
continue
24+
} else if length < 3 {
25+
// 長度為2且字頭字尾相同, 則為回文
26+
dp[start][end] = true
27+
} else {
28+
// 狀態轉移 : 去掉字頭字尾, 判斷是否還是回文
29+
dp[start][end] = dp[start+1][end-1]
30+
}
31+
if dp[start][end] && (end-start+1) > len(result) {
32+
result = s[start : end+1]
33+
}
34+
}
35+
}
36+
return result
37+
}
38+
39+
// @lc code=end
40+

0 commit comments

Comments
 (0)