Skip to content

Commit 0a32aeb

Browse files
committed
feat: add solutions to lc problem: No.0005. Longest Palindromic
Substring
1 parent 008f877 commit 0a32aeb

File tree

11 files changed

+371
-88
lines changed

11 files changed

+371
-88
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
- [不同路径](./solution/0000-0099/0062.Unique%20Paths/README.md)
193193
- [不同路径 II](./solution/0000-0099/0063.Unique%20Paths%20II/README.md)
194194
- [最小路径和](./solution/0000-0099/0064.Minimum%20Path%20Sum/README.md)
195+
- [最长回文子串](./solution/0000-0099/0005.Longest%20Palindromic%20Substring/README.md)
195196
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
196197
- [最长上升子序列](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)
197198
- [俄罗斯套娃信封问题](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
186186
- [Unique Paths](./solution/0000-0099/0062.Unique%20Paths/README_EN.md)
187187
- [Unique Paths II](./solution/0000-0099/0063.Unique%20Paths%20II/README_EN.md)
188188
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)
189+
- [Longest Palindromic Substring](./solution/0000-0099/0005.Longest%20Palindromic%20Substring/README_EN.md)
189190
- [Longest Increasing Subsequence](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md)
190191
- [Russian Doll Envelopes](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)
191192
- [Longest Common Subsequence](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md)

lcof/面试题52. 两个链表的第一个公共节点/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,30 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
182182
}
183183
```
184184

185+
### **C#**
186+
187+
```cs
188+
/**
189+
* Definition for singly-linked list.
190+
* public class ListNode {
191+
* public int val;
192+
* public ListNode next;
193+
* public ListNode(int x) { val = x; }
194+
* }
195+
*/
196+
public class Solution {
197+
public ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
198+
ListNode cur1 = headA, cur2 = headB;
199+
while (cur1 != cur2)
200+
{
201+
cur1 = cur1 == null ? headB : cur1.next;
202+
cur2 = cur2 == null ? headA : cur2.next;
203+
}
204+
return cur1;
205+
}
206+
}
207+
```
208+
185209
### **...**
186210

187211
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
public class Solution {
10+
public ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
11+
ListNode cur1 = headA, cur2 = headB;
12+
while (cur1 != cur2)
13+
{
14+
cur1 = cur1 == null ? headB : cur1.next;
15+
cur2 = cur2 == null ? headA : cur2.next;
16+
}
17+
return cur1;
18+
}
19+
}

solution/0000-0099/0005.Longest Palindromic Substring/README.md

+126-2
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,151 @@
4848
<li><code>s</code> 仅由数字和英文字母(大写和/或小写)组成</li>
4949
</ul>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
5554

55+
动态规划法。
56+
57+
`dp[i][j]` 表示字符串 `s[i..j]` 是否为回文串。
58+
59+
-`j - i < 2`,即字符串长度为 2 时,只要 `s[i] == s[j]``dp[i][j]` 就为 true。
60+
-`j - i >= 2``dp[i][j] = dp[i + 1][j - 1] && s[i] == s[j]`
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

6066
<!-- 这里可写当前语言的特殊实现逻辑 -->
6167

6268
```python
63-
69+
class Solution:
70+
def longestPalindrome(self, s: str) -> str:
71+
n = len(s)
72+
dp = [[False] * n for _ in range(n)]
73+
start, mx = 0, 1
74+
for j in range(n):
75+
for i in range(j + 1):
76+
if j - i < 2:
77+
dp[i][j] = s[i] == s[j]
78+
else:
79+
dp[i][j] = dp[i + 1][j - 1] and s[i] == s[j]
80+
if dp[i][j] and mx < j - i + 1:
81+
start, mx = i, j - i + 1
82+
return s[start:start+mx]
6483
```
6584

6685
### **Java**
6786

6887
<!-- 这里可写当前语言的特殊实现逻辑 -->
6988

7089
```java
90+
class Solution {
91+
public String longestPalindrome(String s) {
92+
int n = s.length();
93+
boolean[][] dp = new boolean[n][n];
94+
int mx = 1, start = 0;
95+
for (int j = 0; j < n; ++j) {
96+
for (int i = 0; i <= j; ++i) {
97+
if (j - i < 2) {
98+
dp[i][j] = s.charAt(i) == s.charAt(j);
99+
} else {
100+
dp[i][j] = dp[i + 1][j - 1] && s.charAt(i) == s.charAt(j);
101+
}
102+
if (dp[i][j] && mx < j - i + 1) {
103+
mx = j - i + 1;
104+
start = i;
105+
}
106+
}
107+
}
108+
return s.substring(start, start + mx);
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
string longestPalindrome(string s) {
119+
int n = s.size();
120+
vector<vector<bool>> dp(n, vector<bool>(n, false));
121+
int start = 0, mx = 1;
122+
for (int j = 0; j < n; ++j) {
123+
for (int i = 0; i <= j; ++i) {
124+
if (j - i < 2) {
125+
dp[i][j] = s[i] == s[j];
126+
} else {
127+
dp[i][j] = dp[i + 1][j - 1] && s[i] == s[j];
128+
}
129+
if (dp[i][j] && mx < j - i + 1) {
130+
start = i;
131+
mx = j - i + 1;
132+
}
133+
}
134+
}
135+
return s.substr(start, mx);
136+
}
137+
};
138+
```
139+
140+
### **Go**
141+
142+
```go
143+
func longestPalindrome(s string) string {
144+
n := len(s)
145+
dp := make([][]bool, n)
146+
for i := 0; i < n; i++ {
147+
dp[i] = make([]bool, n)
148+
}
149+
mx, start := 1, 0
150+
for j := 0; j < n; j++ {
151+
for i := 0; i <= j; i++ {
152+
if j-i < 2 {
153+
dp[i][j] = s[i] == s[j]
154+
} else {
155+
dp[i][j] = dp[i+1][j-1] && s[i] == s[j]
156+
}
157+
if dp[i][j] && mx < j-i+1 {
158+
mx, start = j-i+1, i
159+
}
160+
}
161+
}
162+
return s[start : start+mx]
163+
}
164+
```
71165

166+
### **C#**
167+
168+
```cs
169+
public class Solution{
170+
public string LongestPalindrome(string s) {
171+
int n = s.Length;
172+
bool[,] dp = new bool[n, n];
173+
int mx = 1, start = 0;
174+
for (int j = 0; j < n; ++j)
175+
{
176+
for (int i = 0; i <= j; ++i)
177+
{
178+
if (j - i < 2)
179+
{
180+
dp[i, j] = s[i] == s[j];
181+
}
182+
else
183+
{
184+
dp[i, j] = dp[i + 1, j - 1] && s[i] == s[j];
185+
}
186+
if (dp[i, j] && mx < j - i + 1)
187+
{
188+
mx = j - i + 1;
189+
start = i;
190+
}
191+
}
192+
}
193+
return s.Substring(start, mx);
194+
}
195+
}
72196
```
73197

74198
### **...**

solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md

+121-1
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,138 @@
8181

8282
## Solutions
8383

84+
Dynamic programming.
85+
8486
<!-- tabs:start -->
8587

8688
### **Python3**
8789

8890
```python
89-
91+
class Solution:
92+
def longestPalindrome(self, s: str) -> str:
93+
n = len(s)
94+
dp = [[False] * n for _ in range(n)]
95+
start, mx = 0, 1
96+
for j in range(n):
97+
for i in range(j + 1):
98+
if j - i < 2:
99+
dp[i][j] = s[i] == s[j]
100+
else:
101+
dp[i][j] = dp[i + 1][j - 1] and s[i] == s[j]
102+
if dp[i][j] and mx < j - i + 1:
103+
start, mx = i, j - i + 1
104+
return s[start:start+mx]
90105
```
91106

92107
### **Java**
93108

94109
```java
110+
class Solution {
111+
public String longestPalindrome(String s) {
112+
int n = s.length();
113+
boolean[][] dp = new boolean[n][n];
114+
int mx = 1, start = 0;
115+
for (int j = 0; j < n; ++j) {
116+
for (int i = 0; i <= j; ++i) {
117+
if (j - i < 2) {
118+
dp[i][j] = s.charAt(i) == s.charAt(j);
119+
} else {
120+
dp[i][j] = dp[i + 1][j - 1] && s.charAt(i) == s.charAt(j);
121+
}
122+
if (dp[i][j] && mx < j - i + 1) {
123+
mx = j - i + 1;
124+
start = i;
125+
}
126+
}
127+
}
128+
return s.substring(start, start + mx);
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
string longestPalindrome(string s) {
139+
int n = s.size();
140+
vector<vector<bool>> dp(n, vector<bool>(n, false));
141+
int start = 0, mx = 1;
142+
for (int j = 0; j < n; ++j) {
143+
for (int i = 0; i <= j; ++i) {
144+
if (j - i < 2) {
145+
dp[i][j] = s[i] == s[j];
146+
} else {
147+
dp[i][j] = dp[i + 1][j - 1] && s[i] == s[j];
148+
}
149+
if (dp[i][j] && mx < j - i + 1) {
150+
start = i;
151+
mx = j - i + 1;
152+
}
153+
}
154+
}
155+
return s.substr(start, mx);
156+
}
157+
};
158+
```
159+
160+
### **Go**
161+
162+
```go
163+
func longestPalindrome(s string) string {
164+
n := len(s)
165+
dp := make([][]bool, n)
166+
for i := 0; i < n; i++ {
167+
dp[i] = make([]bool, n)
168+
}
169+
mx, start := 1, 0
170+
for j := 0; j < n; j++ {
171+
for i := 0; i <= j; i++ {
172+
if j-i < 2 {
173+
dp[i][j] = s[i] == s[j]
174+
} else {
175+
dp[i][j] = dp[i+1][j-1] && s[i] == s[j]
176+
}
177+
if dp[i][j] && mx < j-i+1 {
178+
mx, start = j-i+1, i
179+
}
180+
}
181+
}
182+
return s[start : start+mx]
183+
}
184+
```
95185

186+
### **C#**
187+
188+
```cs
189+
public class Solution{
190+
public string LongestPalindrome(string s) {
191+
int n = s.Length;
192+
bool[,] dp = new bool[n, n];
193+
int mx = 1, start = 0;
194+
for (int j = 0; j < n; ++j)
195+
{
196+
for (int i = 0; i <= j; ++i)
197+
{
198+
if (j - i < 2)
199+
{
200+
dp[i, j] = s[i] == s[j];
201+
}
202+
else
203+
{
204+
dp[i, j] = dp[i + 1, j - 1] && s[i] == s[j];
205+
}
206+
if (dp[i, j] && mx < j - i + 1)
207+
{
208+
mx = j - i + 1;
209+
start = i;
210+
}
211+
}
212+
}
213+
return s.Substring(start, mx);
214+
}
215+
}
96216
```
97217

98218
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
string longestPalindrome(string s) {
4+
int n = s.size();
5+
vector<vector<bool>> dp(n, vector<bool>(n, false));
6+
int start = 0, mx = 1;
7+
for (int j = 0; j < n; ++j) {
8+
for (int i = 0; i <= j; ++i) {
9+
if (j - i < 2) {
10+
dp[i][j] = s[i] == s[j];
11+
} else {
12+
dp[i][j] = dp[i + 1][j - 1] && s[i] == s[j];
13+
}
14+
if (dp[i][j] && mx < j - i + 1) {
15+
start = i;
16+
mx = j - i + 1;
17+
}
18+
}
19+
}
20+
return s.substr(start, mx);
21+
}
22+
};

0 commit comments

Comments
 (0)