Skip to content

Commit 8683682

Browse files
author
luzhipeng
committed
1 parent a15d18f commit 8683682

14 files changed

+248
-69
lines changed

README.en.md

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ The data structures mainly includes:
125125

126126
- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
127127
- [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md)
128+
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md) 🆕
128129
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
129130
- [0015.3-sum](./problems/15.3-sum.md)
130131
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
@@ -173,6 +174,7 @@ The data structures mainly includes:
173174
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
174175
- [0454.4-sum-ii](./problems/454.4-sum-ii.md)
175176
- [0494.target-sum](./problems/494.target-sum.md)
177+
- [0516.longest-palindromic-subsequence](./problems/516.longest-palindromic-subsequence.md) 🆕
176178
- [0518.coin-change-2](./problems/518.coin-change-2.md)
177179
- [0609.find-duplicate-file-in-system](./problems/609.find-duplicate-file-in-system.md) 🆕
178180
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
123123

124124
- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
125125
- [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md)
126+
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md) 🆕
126127
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
127128
- [0015.3-sum](./problems/15.3-sum.md)
128129
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
@@ -171,6 +172,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
171172
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
172173
- [0454.4-sum-ii](./problems/454.4-sum-ii.md)
173174
- [0494.target-sum](./problems/494.target-sum.md)
175+
- [0516.longest-palindromic-subsequence](./problems/516.longest-palindromic-subsequence.md) 🆕
174176
- [0518.coin-change-2](./problems/518.coin-change-2.md)
175177
- [0609.find-duplicate-file-in-system](./problems/609.find-duplicate-file-in-system.md) 🆕
176178
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-06-04T02:54:18.675Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" etag="P6JRC3FKFQoxBOlXs-oj" version="10.7.4" type="device"><diagram id="b4K2J2u3Fau-mtGLlD6n" name="第 1 页">7Zpdb5swFIZ/TS5bBRsTetm0ZZvUSps6bZeTAw5YNZiB06T79TsG0+A40zY1gXRrLxrz2vjjOefYHJIJvso37ypaZncyYWKCpslmgq8nCAXIg/9aeGqF8AK3QlrxpJW8rXDPfzAjTo264gmrrYZKSqF4aYuxLAoWK0ujVSXXdrOlFPaoJU2ZI9zHVLjqV56ozKwCzbb6e8bTrBvZCy7ampx2jc1K6owmct2T8M0EX1VSqraUb66Y0Ow6Lu190S9qnydWsUL9yQ0SX9wWn5IP2SOq392xL+8FCc5ML49UrMyCF2a26qlDUMlVkTDdy3SC5+uMK3Zf0ljXrsHmoGUqF3DlQdH0xyrFNr+cqPe8fHAbJnOmqidoYm5AU0PMuIzXEVxvDUCMlPXYdxo1Jk+fe95SgYIB8xeQkAOJjg+JnBgkfIKehE/Nk/wT9CR8ap5EHEjJ6JD8U/OkwIFUUsGLpJI5jxtWFS/S0bnt7uV4DzcIUxdccCxws0PvU0suxJUUsmruxQlhYeKDDhaQD6xXE6IFDoLDcPX833MNhsQa/hNYfXxiWC9eZ5j7ZOww7/KHoznkMoxZHO9zyEVI9PqPEuf7wA7qkJ6bMbzwdB6F626gj8/VTTIcrJBBlrrI8yZp7UPUMDhkrZeCpwVoSpY99ZYumPgoa6641LULqZTMoYHQFXMaP6SNyfpmaP6gSTPYZV22ybXGT7uLJd9oI8/NfK4zpXRWfqlJoChOCnzOIS9fwn7FqvMYRkRRQhU88kZar+FzKag6o7GeV33WiHrLi4jGEX3m8cO3O1o9AJ3wvOz2uRcZnnhkZ4cnjuEHfZDzDp44jXJyotDmSoKxA8rNtd4CaoCA2mf4YQPKzR9fZUARm2swGzug3JTzLaAGCKh9hh82oP6NlBn/AddhA+rYOfMwj9KIhBbXWTgyV+Smfq8xRcHeqXF1U7//4gCoGKNlKWCWzdxQ1OzzEfI3CJ4xo2smmGIH2/xto/tk5PSks/Gb0Y9pdPtlxL5IH9bobk46QYGAYecJf7SMH3xf6S+w50tZqLO6+foeOE9h+ZtmG+zqoZSaz6afuqTFyzoiczIhcOP0VhYpqxWUPlrvRu9Xi+71aDsmsGiHtaeira9X1ak73g12VbZT29t+IQu2c0YYiRqnj8EPWLUnGnKeJHqYveePfUJpLObXEai77oXF7OYyuDnUO0cUWO6IfNcd/e6hsO+P3t/7I1xufwHR1PV+RoJvfgI=</diagram></mxfile>
Loading
Loading
Loading
Loading
Loading
Loading

problems/5.longest-palindromic-substring.js

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## 题目地址
2+
3+
https://leetcode.com/problems/longest-palindromic-substring/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
9+
10+
Example 1:
11+
12+
Input: "babad"
13+
Output: "bab"
14+
Note: "aba" is also a valid answer.
15+
Example 2:
16+
17+
Input: "cbbd"
18+
Output: "bb"
19+
```
20+
21+
## 思路
22+
23+
这是一道最长回文的题目,要我们求出给定字符串的最大回文子串。
24+
25+
![5.longest-palindromic-substring](../assets/problems/5.longest-palindromic-substring-1.png)
26+
27+
解决这类问题的核心思想就是两个字“延伸”,具体来说
28+
29+
- 如果一个字符串是回文串,那么在它左右分别加上一个相同的字符,那么它一定还是一个回文串
30+
- 如果一个字符串不是回文串,或者在回文串左右分别加不同的字符,得到的一定不是回文串
31+
32+
事实上,上面的分析已经建立了大问题和小问题之间的关联,
33+
基于此,我们可以建立动态规划模型。
34+
35+
我们可以用 dp[i][j] 表示 s 中从 i 到 j(包括 i 和 j)是否可以形成回文,
36+
状态转移方程只是将上面的描述转化为代码即可:
37+
38+
```js
39+
if (s[i] === s[j] && dp[i + 1][j - 1]) {
40+
dp[i][j] = true;
41+
}
42+
```
43+
![5.longest-palindromic-substring-2](../assets/problems/5.longest-palindromic-substring-2.png)
44+
45+
base case就是一个字符(轴对称点是本身),或者两个字符(轴对称点是介于两者之间的虚拟点)。
46+
47+
![5.longest-palindromic-substring-3](../assets/problems/5.longest-palindromic-substring-3.png)
48+
## 关键点
49+
50+
- ”延伸“(extend)
51+
52+
## 代码
53+
54+
```js
55+
/*
56+
* @lc app=leetcode id=5 lang=javascript
57+
*
58+
* [5] Longest Palindromic Substring
59+
*/
60+
/**
61+
* @param {string} s
62+
* @return {string}
63+
*/
64+
var longestPalindrome = function(s) {
65+
// babad
66+
// tag : dp
67+
if (!s || s.length === 0) return "";
68+
let res = s[0];
69+
70+
const dp = [];
71+
72+
// 倒着遍历简化操作, 这么做的原因是dp[i][..]依赖于dp[i + 1][..]
73+
for (let i = s.length - 1; i >= 0; i--) {
74+
dp[i] = [];
75+
for (let j = i; j < s.length; j++) {
76+
if (j - i === 0) dp[i][j] = true;
77+
// specail case 1
78+
else if (j - i === 1 && s[i] === s[j]) dp[i][j] = true;
79+
// specail case 2
80+
else if (s[i] === s[j] && dp[i + 1][j - 1]) {
81+
// state transition
82+
dp[i][j] = true;
83+
}
84+
85+
if (dp[i][j] && j - i + 1 > res.length) {
86+
// update res
87+
res = s.slice(i, j + 1);
88+
}
89+
}
90+
}
91+
92+
return res;
93+
};
94+
```
95+
96+
## 相关题目
97+
98+
-[516.longest-palindromic-subsequence](./516.longest-palindromic-subsequence.md)

problems/516.longest-palindromic-subsequence.js

-29
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
## 题目地址
2+
3+
https://leetcode.com/problems/longest-palindromic-subsequence/description/
4+
5+
## 题目描述
6+
7+
```
8+
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
9+
10+
Example 1:
11+
Input:
12+
13+
"bbbab"
14+
Output:
15+
4
16+
One possible longest palindromic subsequence is "bbbb".
17+
Example 2:
18+
Input:
19+
20+
"cbbd"
21+
Output:
22+
2
23+
One possible longest palindromic subsequence is "bb".
24+
```
25+
26+
## 思路
27+
28+
这是一道最长回文的题目,要我们求出给定字符串的最大回文子序列。
29+
30+
![516.longest-palindromic-subsequence-1](../assets/problems/516.longest-palindromic-subsequence-1.png)
31+
32+
解决这类问题的核心思想就是两个字“延伸”,具体来说
33+
34+
- 如果一个字符串是回文串,那么在它左右分别加上一个相同的字符,那么它一定还是一个回文串,因此`回文长度增加2`
35+
- 如果一个字符串不是回文串,或者在回文串左右分别加不同的字符,得到的一定不是回文串,因此`回文长度不变,我们取[i][j-1]和[i+1][j]的较大值`
36+
37+
![516.longest-palindromic-subsequence-2](../assets/problems/516.longest-palindromic-subsequence-2.png)
38+
39+
事实上,上面的分析已经建立了大问题和小问题之间的关联,
40+
基于此,我们可以建立动态规划模型。
41+
42+
我们可以用 dp[i][j] 表示 s 中从 i 到 j(包括 i 和 j)的回文序列长度,
43+
状态转移方程只是将上面的描述转化为代码即可:
44+
45+
```js
46+
if (s[i] === s[j]) {
47+
dp[i][j] = dp[i + 1][j - 1] + 2;
48+
} else {
49+
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
50+
}
51+
```
52+
53+
base case 就是一个字符(轴对称点是本身)
54+
55+
![516.longest-palindromic-subsequence-3](../assets/problems/516.longest-palindromic-subsequence-3.png)
56+
57+
## 关键点
58+
59+
- ”延伸“(extend)
60+
61+
## 代码
62+
63+
```js
64+
/*
65+
* @lc app=leetcode id=516 lang=javascript
66+
*
67+
* [516] Longest Palindromic Subsequence
68+
*/
69+
/**
70+
* @param {string} s
71+
* @return {number}
72+
*/
73+
var longestPalindromeSubseq = function(s) {
74+
// bbbab 返回4
75+
// tag : dp
76+
const dp = [];
77+
78+
for (let i = s.length - 1; i >= 0; i--) {
79+
dp[i] = Array(s.length).fill(0);
80+
for (let j = i; j < s.length; j++) {
81+
if (i - j === 0) dp[i][j] = 1;
82+
else if (s[i] === s[j]) {
83+
dp[i][j] = dp[i + 1][j - 1] + 2;
84+
} else {
85+
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
86+
}
87+
}
88+
}
89+
90+
return dp[0][s.length - 1];
91+
};
92+
```
93+
94+
## 相关题目
95+
96+
-[5.longest-palindromic-substring](./5.longest-palindromic-substring.md)

thinkings/string-problems.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## 字符串问题
2+
3+
字符串问题有很多,从简单的实现substr,识别回文,到复杂一点的公共子串/子序列。其实字符串本质上也是字符数组,因此
4+
很多数据的思想和方法也可以用在字符串问题上,并且在有些时候能够发挥很好的作用。
5+
6+
专门处理字符串的算法也很多,比如trie,马拉车算法,游程编码,huffman树等等。
7+
8+
9+
## 实现字符串的一些原生方法
10+
11+
这类题目应该是最直接的题目了,题目歧义比较小, 难度也是相对较小,因此用于电面等形式也是不错的。
12+
13+
- [28.implement-str-str](https://leetcode.com/problems/implement-strstr/)
14+
- [344.reverse-string](../backlog/344.reverse-string.js)
15+
16+
## 回文
17+
18+
回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。
19+
20+
判断是否回文的通用方法是首尾双指针,具体可以见下方125号题目。 判断最长回文的思路主要是两个字"扩展",
21+
如果可以充分利用回文的特点,则可以减少很多无谓的计算,典型的是《马拉车算法》。
22+
23+
24+
### 相关问题
25+
26+
- [5.longest-palindromic-substring](../problems/5.longest-palindromic-substring.md)
27+
28+
- [125.valid-palindrome](../problems/125.valid-palindrome.md)
29+
30+
- [131.palindrome-partitioning](../problems/131.palindrome-partitioning.md)
31+
32+
- [shortest-palindrome](https://leetcode.com/problems/shortest-palindrome/)
33+
34+
- [516.longest-palindromic-subsequence](../problems/516.longest-palindromic-subsequence.md)
35+
36+
37+
## 前缀问题
38+
39+
前缀树用来处理这种问题是最符合直觉的,但是它也有缺点,比如公共前缀很少的情况下,比较费内存。
40+
41+
### 相关题目
42+
43+
-[14.longest-common-prefix](../14.longest-common-prefix.js)
44+
-[208.implement-trie-prefix-tree](../problems/208.implement-trie-prefix-tree.md)
45+
46+
47+
## 其他问题
48+
49+
- [139.word-break](../problems/139.word-break.md)

0 commit comments

Comments
 (0)