Skip to content

Commit 22b56de

Browse files
zhouboyi1998zhouboyi
authored andcommitted
docs(note): 添加题解:5. 最长回文子串
1 parent a6462f1 commit 22b56de

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

markdown/hello/0000.Hello.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
| <span style="color: #FFB822;">**Medium**</span> | [**0002.两数相加**](../medium/0002.两数相加.md) |
77
| <span style="color: #FFB822;">**Medium**</span> | [**0003.无重复字符的最长子串**](../medium/0003.无重复字符的最长子串.md) |
88
| <span style="color: #FF2D55;">**Hard**</span> | [**0004.寻找两个正序数组的中位数**](../hard/0004.寻找两个正序数组的中位数.md) |
9+
| <span style="color: #FFB822;">**Medium**</span> | [**0005.最长回文子串**](../medium/0005.最长回文子串.md) |
910
| <span style="color: #FFB822;">**Medium**</span> | [**0007.整数反转**](../medium/0007.整数反转.md) |
1011
| <span style="color: #00AF9B;">**Easy**</span> | [**0009.回文数**](../easy/0009.回文数.md) |
1112
| <span style="color: #FFB822;">**Medium**</span> | [**0011.盛最多水的容器**](../medium/0011.盛最多水的容器.md) |
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<h1 style="text-align: center;"> <span style="color: #FFB822;">5. 最长回文子串</span> </h1>
2+
3+
### 🚀 LeetCode
4+
5+
<base target="_blank">
6+
7+
<span style="color: #FFB822;">**Medium**</span> [**https://leetcode.cn/problems/longest-palindromic-substring/**](https://leetcode.cn/problems/longest-palindromic-substring/)
8+
9+
---
10+
11+
### ❓ Description
12+
13+
<br/>
14+
15+
给你一个字符串 `s`,找到 `s` 中最长的 **回文子串**
16+
17+
<br/>
18+
19+
**示例 1:**
20+
21+
```
22+
输入: s = "babad"
23+
输出: "bab"
24+
解释: "aba" 同样是符合题意的答案
25+
```
26+
27+
**示例 2:**
28+
29+
```
30+
输入: s = "cbbd"
31+
输出: "bb"
32+
```
33+
34+
<br/>
35+
36+
**提示:**
37+
38+
* `1 <= s.length <= 1000`
39+
* `s` 仅由数字和英文字母组成
40+
41+
---
42+
43+
### ❗ Solution(中心扩展法)
44+
45+
<br/>
46+
47+
#### idea
48+
49+
* 时间复杂度:`O(n^2)`
50+
* 空间复杂度:`O(1)`
51+
52+
<br/>
53+
54+
#### Java
55+
56+
```
57+
class Solution {
58+
public String longestPalindrome(String s) {
59+
int length = s.length();
60+
if (s == null || length == 0) {
61+
return "";
62+
}
63+
if (length == 1) {
64+
return s;
65+
}
66+
67+
// 存储最长回文子串
68+
String max = "";
69+
70+
for (int i = 0; i < length; i++) {
71+
// 以 1 个字符为中心, 向两边扩展
72+
String sub1 = expandAroundCenter(s, i, i);
73+
// 以 2 个字符为中心, 向两边扩展
74+
String sub2 = expandAroundCenter(s, i, i + 1);
75+
// 比较本轮的两个回文子串的长度
76+
String sub = sub1.length() > sub2.length() ? sub1 : sub2;
77+
// 比较本轮的回文子串和之前轮次的回文子串的长度
78+
if (sub.length() > max.length()) {
79+
max = sub;
80+
}
81+
}
82+
83+
return max;
84+
}
85+
86+
// 中心扩展法
87+
public String expandAroundCenter(String s, int left, int right) {
88+
int length = s.length();
89+
while (left >= 0 && right < length && s.charAt(left) == s.charAt(right)) {
90+
left--;
91+
right++;
92+
}
93+
return s.substring(left + 1, right);
94+
}
95+
}
96+
```

0 commit comments

Comments
 (0)