Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Jun 4, 2019
1 parent a15d18f commit 8683682
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 69 deletions.
2 changes: 2 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ The data structures mainly includes:

- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
- [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md)
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md) 🆕
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
- [0015.3-sum](./problems/15.3-sum.md)
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
Expand Down Expand Up @@ -173,6 +174,7 @@ The data structures mainly includes:
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
- [0454.4-sum-ii](./problems/454.4-sum-ii.md)
- [0494.target-sum](./problems/494.target-sum.md)
- [0516.longest-palindromic-subsequence](./problems/516.longest-palindromic-subsequence.md) 🆕
- [0518.coin-change-2](./problems/518.coin-change-2.md)
- [0609.find-duplicate-file-in-system](./problems/609.find-duplicate-file-in-system.md) 🆕
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。

- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
- [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md)
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md) 🆕
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
- [0015.3-sum](./problems/15.3-sum.md)
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
Expand Down Expand Up @@ -171,6 +172,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
- [0454.4-sum-ii](./problems/454.4-sum-ii.md)
- [0494.target-sum](./problems/494.target-sum.md)
- [0516.longest-palindromic-subsequence](./problems/516.longest-palindromic-subsequence.md) 🆕
- [0518.coin-change-2](./problems/518.coin-change-2.md)
- [0609.find-duplicate-file-in-system](./problems/609.find-duplicate-file-in-system.md) 🆕
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
Expand Down
1 change: 1 addition & 0 deletions assets/drawio/5.longest-palindromic-substring.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 0 additions & 40 deletions problems/5.longest-palindromic-substring.js

This file was deleted.

98 changes: 98 additions & 0 deletions problems/5.longest-palindromic-substring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
## 题目地址

https://leetcode.com/problems/longest-palindromic-substring/description/

## 题目描述

```
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
```

## 思路

这是一道最长回文的题目,要我们求出给定字符串的最大回文子串。

![5.longest-palindromic-substring](../assets/problems/5.longest-palindromic-substring-1.png)

解决这类问题的核心思想就是两个字“延伸”,具体来说

- 如果一个字符串是回文串,那么在它左右分别加上一个相同的字符,那么它一定还是一个回文串
- 如果一个字符串不是回文串,或者在回文串左右分别加不同的字符,得到的一定不是回文串

事实上,上面的分析已经建立了大问题和小问题之间的关联,
基于此,我们可以建立动态规划模型。

我们可以用 dp[i][j] 表示 s 中从 i 到 j(包括 i 和 j)是否可以形成回文,
状态转移方程只是将上面的描述转化为代码即可:

```js
if (s[i] === s[j] && dp[i + 1][j - 1]) {
dp[i][j] = true;
}
```
![5.longest-palindromic-substring-2](../assets/problems/5.longest-palindromic-substring-2.png)

base case就是一个字符(轴对称点是本身),或者两个字符(轴对称点是介于两者之间的虚拟点)。

![5.longest-palindromic-substring-3](../assets/problems/5.longest-palindromic-substring-3.png)
## 关键点

- ”延伸“(extend)

## 代码

```js
/*
* @lc app=leetcode id=5 lang=javascript
*
* [5] Longest Palindromic Substring
*/
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
// babad
// tag : dp
if (!s || s.length === 0) return "";
let res = s[0];

const dp = [];

// 倒着遍历简化操作, 这么做的原因是dp[i][..]依赖于dp[i + 1][..]
for (let i = s.length - 1; i >= 0; i--) {
dp[i] = [];
for (let j = i; j < s.length; j++) {
if (j - i === 0) dp[i][j] = true;
// specail case 1
else if (j - i === 1 && s[i] === s[j]) dp[i][j] = true;
// specail case 2
else if (s[i] === s[j] && dp[i + 1][j - 1]) {
// state transition
dp[i][j] = true;
}

if (dp[i][j] && j - i + 1 > res.length) {
// update res
res = s.slice(i, j + 1);
}
}
}

return res;
};
```

## 相关题目

-[516.longest-palindromic-subsequence](./516.longest-palindromic-subsequence.md)
29 changes: 0 additions & 29 deletions problems/516.longest-palindromic-subsequence.js

This file was deleted.

96 changes: 96 additions & 0 deletions problems/516.longest-palindromic-subsequence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
## 题目地址

https://leetcode.com/problems/longest-palindromic-subsequence/description/

## 题目描述

```
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
Example 1:
Input:
"bbbab"
Output:
4
One possible longest palindromic subsequence is "bbbb".
Example 2:
Input:
"cbbd"
Output:
2
One possible longest palindromic subsequence is "bb".
```

## 思路

这是一道最长回文的题目,要我们求出给定字符串的最大回文子序列。

![516.longest-palindromic-subsequence-1](../assets/problems/516.longest-palindromic-subsequence-1.png)

解决这类问题的核心思想就是两个字“延伸”,具体来说

- 如果一个字符串是回文串,那么在它左右分别加上一个相同的字符,那么它一定还是一个回文串,因此`回文长度增加2`
- 如果一个字符串不是回文串,或者在回文串左右分别加不同的字符,得到的一定不是回文串,因此`回文长度不变,我们取[i][j-1]和[i+1][j]的较大值`

![516.longest-palindromic-subsequence-2](../assets/problems/516.longest-palindromic-subsequence-2.png)

事实上,上面的分析已经建立了大问题和小问题之间的关联,
基于此,我们可以建立动态规划模型。

我们可以用 dp[i][j] 表示 s 中从 i 到 j(包括 i 和 j)的回文序列长度,
状态转移方程只是将上面的描述转化为代码即可:

```js
if (s[i] === s[j]) {
dp[i][j] = dp[i + 1][j - 1] + 2;
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
```

base case 就是一个字符(轴对称点是本身)

![516.longest-palindromic-subsequence-3](../assets/problems/516.longest-palindromic-subsequence-3.png)

## 关键点

- ”延伸“(extend)

## 代码

```js
/*
* @lc app=leetcode id=516 lang=javascript
*
* [516] Longest Palindromic Subsequence
*/
/**
* @param {string} s
* @return {number}
*/
var longestPalindromeSubseq = function(s) {
// bbbab 返回4
// tag : dp
const dp = [];

for (let i = s.length - 1; i >= 0; i--) {
dp[i] = Array(s.length).fill(0);
for (let j = i; j < s.length; j++) {
if (i - j === 0) dp[i][j] = 1;
else if (s[i] === s[j]) {
dp[i][j] = dp[i + 1][j - 1] + 2;
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
}
}

return dp[0][s.length - 1];
};
```

## 相关题目

-[5.longest-palindromic-substring](./5.longest-palindromic-substring.md)
49 changes: 49 additions & 0 deletions thinkings/string-problems.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## 字符串问题

字符串问题有很多,从简单的实现substr,识别回文,到复杂一点的公共子串/子序列。其实字符串本质上也是字符数组,因此
很多数据的思想和方法也可以用在字符串问题上,并且在有些时候能够发挥很好的作用。

专门处理字符串的算法也很多,比如trie,马拉车算法,游程编码,huffman树等等。


## 实现字符串的一些原生方法

这类题目应该是最直接的题目了,题目歧义比较小, 难度也是相对较小,因此用于电面等形式也是不错的。

- [28.implement-str-str](https://leetcode.com/problems/implement-strstr/)
- [344.reverse-string](../backlog/344.reverse-string.js)

## 回文

回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。

判断是否回文的通用方法是首尾双指针,具体可以见下方125号题目。 判断最长回文的思路主要是两个字"扩展",
如果可以充分利用回文的特点,则可以减少很多无谓的计算,典型的是《马拉车算法》。


### 相关问题

- [5.longest-palindromic-substring](../problems/5.longest-palindromic-substring.md)

- [125.valid-palindrome](../problems/125.valid-palindrome.md)

- [131.palindrome-partitioning](../problems/131.palindrome-partitioning.md)

- [shortest-palindrome](https://leetcode.com/problems/shortest-palindrome/)

- [516.longest-palindromic-subsequence](../problems/516.longest-palindromic-subsequence.md)


## 前缀问题

前缀树用来处理这种问题是最符合直觉的,但是它也有缺点,比如公共前缀很少的情况下,比较费内存。

### 相关题目

-[14.longest-common-prefix](../14.longest-common-prefix.js)
-[208.implement-trie-prefix-tree](../problems/208.implement-trie-prefix-tree.md)


## 其他问题

- [139.word-break](../problems/139.word-break.md)

0 comments on commit 8683682

Please sign in to comment.