Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Jun 15, 2021
2 parents 7f76ec2 + 3461b91 commit 5f55278
Show file tree
Hide file tree
Showing 36 changed files with 1,425 additions and 78 deletions.
2 changes: 1 addition & 1 deletion problems/0001.两数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public int[] twoSum(int[] nums, int target) {

Python:

```python3
```python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap={}
Expand Down
16 changes: 16 additions & 0 deletions problems/0070.爬楼梯完全背包版本.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ class Solution {
Python:


```python3
class Solution:
def climbStairs(self, n: int) -> int:
dp = [0]*(n + 1)
dp[0] = 1
m = 2
# 遍历背包
for j in range(n + 1):
# 遍历物品
for step in range(1, m + 1):
if j >= step:
dp[j] += dp[j - step]
return dp[n]
```


Go:
```go
func climbStairs(n int) int {
Expand Down
142 changes: 91 additions & 51 deletions problems/0072.编辑距离.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## 72. 编辑距离

https://leetcode-cn.com/problems/edit-distance/
https://leetcode-cn.com/problems/edit-distance/

给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

Expand All @@ -18,23 +18,23 @@ https://leetcode-cn.com/problems/edit-distance/
* 删除一个字符
* 替换一个字符

示例 1:
输入:word1 = "horse", word2 = "ros"
输出:3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')

示例 2:
输入:word1 = "intention", word2 = "execution"
输出:5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')
示例 1:
输入:word1 = "horse", word2 = "ros"
输出:3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')

示例 2:
输入:word1 = "intention", word2 = "execution"
输出:5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')


提示:
Expand All @@ -51,57 +51,75 @@ exection -> execution (插入 'u')

接下来我依然使用动规五部曲,对本题做一个详细的分析:

1. 确定dp数组(dp table)以及下标的含义
-----------------------

### 1. 确定dp数组(dp table)以及下标的含义

**dp[i][j] 表示以下标i-1为结尾的字符串word1,和以下标j-1为结尾的字符串word2,最近编辑距离为dp[i][j]**

这里在强调一下:为啥要表示下标i-1为结尾的字符串呢,为啥不表示下标i为结尾的字符串呢?

用i来表示也可以! 但我统一以下标i-1为结尾的字符串,在下面的递归公式中会容易理解一点。

2. 确定递推公式
-----------------------

### 2. 确定递推公式

在确定递推公式的时候,首先要考虑清楚编辑的几种操作,整理如下:

* if (word1[i - 1] == word2[j - 1])
* 不操作
* if (word1[i - 1] != word2[j - 1])
*
*
*
```
if (word1[i - 1] == word2[j - 1])
不操作
if (word1[i - 1] != word2[j - 1])
```

也就是如上4种情况。

也就是如上四种情况。
`if (word1[i - 1] == word2[j - 1])` 那么说明不用任何编辑,`dp[i][j]` 就应该是 `dp[i - 1][j - 1]`,即`dp[i][j] = dp[i - 1][j - 1];`

if (word1[i - 1] == word2[j - 1]) 那么说明不用任何编辑,dp[i][j] 就应该是 dp[i - 1][j - 1],即dp[i][j] = dp[i - 1][j - 1];
此时可能有同学有点不明白,为啥要即`dp[i][j] = dp[i - 1][j - 1]`呢?

此时可能有同学有点不明白,为啥要即dp[i][j] = dp[i - 1][j - 1]呢?
那么就在回顾上面讲过的`dp[i][j]`的定义,`word1[i - 1]``word2[j - 1]`相等了,那么就不用编辑了,以下标i-2为结尾的字符串word1和以下标j-2为结尾的字符串`word2`的最近编辑距离`dp[i - 1][j - 1]`就是 `dp[i][j]`了。

那么就在回顾上面讲过的dp[i][j]的定义,word1[i - 1] 与 word2[j - 1]相等了,那么就不用编辑了,以下标i-2为结尾的字符串word1和以下标j-2为结尾的字符串word2的最近编辑距离dp[i - 1][j - 1] 就是 dp[i][j]
在下面的讲解中,如果哪里看不懂,就回想一下`dp[i][j]`的定义,就明白了

在下面的讲解中,如果哪里看不懂,就回想一下dp[i][j]的定义,就明白了。
**在整个动规的过程中,最为关键就是正确理解`dp[i][j]`的定义**

**在整个动规的过程中,最为关键就是正确理解dp[i][j]的定义!**

if (word1[i - 1] != word2[j - 1]),此时就需要编辑了,如何编辑呢?
`if (word1[i - 1] != word2[j - 1])`,此时就需要编辑了,如何编辑呢?

操作一:word1增加一个元素,使其word1[i - 1]与word2[j - 1]相同,那么就是以下标i-2为结尾的word1 与 i-1为结尾的word2的最近编辑距离 加上一个增加元素的操作。
操作一:word1增加一个元素,使其word1[i - 1]与word2[j - 1]相同,那么就是以下标i-2为结尾的word1 与 j-1为结尾的word2的最近编辑距离 加上一个增加元素的操作。

即 dp[i][j] = dp[i - 1][j] + 1;
`dp[i][j] = dp[i - 1][j] + 1;`


操作二:word2添加一个元素,使其word1[i - 1]与word2[j - 1]相同,那么就是以下标i-1为结尾的word1 与 j-2为结尾的word2的最近编辑距离 加上一个增加元素的操作。

即 dp[i][j] = dp[i][j - 1] + 1;
`dp[i][j] = dp[i][j - 1] + 1;`

这里有同学发现了,怎么都是添加元素,删除元素去哪了。

**word2添加一个元素,相当于word1删除一个元素**,例如 word1 = "ad" ,word2 = "a",word2添加一个元素d,也就是相当于word1删除一个元素d,操作数是一样!
**word2添加一个元素,相当于word1删除一个元素**,例如 `word1 = "ad" ,word2 = "a"``word1`删除元素`'d'``word2`添加一个元素`'d'`,变成`word1="a", word2="ad"`, 最终的操作数是一样! dp数组如下图所示意的:

```
a a d
+-----+-----+ +-----+-----+-----+
| 0 | 1 | | 0 | 1 | 2 |
+-----+-----+ ===> +-----+-----+-----+
a | 1 | 0 | a | 1 | 0 | 1 |
+-----+-----+ +-----+-----+-----+
d | 2 | 1 |
+-----+-----+
```

操作三:替换元素,word1替换word1[i - 1],使其与word2[j - 1]相同,此时不用增加元素,那么以下标i-2为结尾的word1j-2为结尾的word2的最近编辑距离 加上一个替换元素的操作。
操作三:替换元素,`word1`替换`word1[i - 1]`,使其与`word2[j - 1]`相同,此时不用增加元素,那么以下标`i-2`为结尾的`word1``j-2`为结尾的`word2`的最近编辑距离 加上一个替换元素的操作。

即 dp[i][j] = dp[i - 1][j - 1] + 1;
`dp[i][j] = dp[i - 1][j - 1] + 1;`

综上,当 if (word1[i - 1] != word2[j - 1]) 时取最小的,即:dp[i][j] = min({dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]}) + 1;
综上,当 `if (word1[i - 1] != word2[j - 1])` 时取最小的,即:`dp[i][j] = min({dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]}) + 1;`

递归公式代码如下:

Expand All @@ -114,9 +132,12 @@ else {
}
```

3. dp数组如何初始化
---

在回顾一下dp[i][j]的定义。
### 3. dp数组如何初始化


再回顾一下dp[i][j]的定义:

**dp[i][j] 表示以下标i-1为结尾的字符串word1,和以下标j-1为结尾的字符串word2,最近编辑距离为dp[i][j]**

Expand All @@ -135,14 +156,16 @@ for (int i = 0; i <= word1.size(); i++) dp[i][0] = i;
for (int j = 0; j <= word2.size(); j++) dp[0][j] = j;
```

4. 确定遍历顺序
-----------------------

### 4. 确定遍历顺序

从如下四个递推公式:

* dp[i][j] = dp[i - 1][j - 1]
* dp[i][j] = dp[i - 1][j - 1] + 1
* dp[i][j] = dp[i][j - 1] + 1
* dp[i][j] = dp[i - 1][j] + 1
* `dp[i][j] = dp[i - 1][j - 1]`
* `dp[i][j] = dp[i - 1][j - 1] + 1`
* `dp[i][j] = dp[i][j - 1] + 1`
* `dp[i][j] = dp[i - 1][j] + 1`

可以看出dp[i][j]是依赖左方,上方和左上方元素的,如图:

Expand All @@ -164,10 +187,12 @@ for (int i = 1; i <= word1.size(); i++) {
}
}
```
-----------------------

5. 举例推导dp数组
### 5. 举例推导dp数组

以示例1,输入:word1 = "horse", word2 = "ros"为例,dp矩阵状态图如下:

以示例1为例,输入:`word1 = "horse", word2 = "ros"`为例,dp矩阵状态图如下:

![72.编辑距离1](https://img-blog.csdnimg.cn/20210114162132300.jpg)

Expand Down Expand Up @@ -195,7 +220,7 @@ public:
};
```
-----------------------
## 其他语言版本
Expand Down Expand Up @@ -228,7 +253,22 @@ public int minDistance(String word1, String word2) {
```

Python:

```python
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
dp = [[0] * (len(word2)+1) for _ in range(len(word1)+1)]
for i in range(len(word1)+1):
dp[i][0] = i
for j in range(len(word2)+1):
dp[0][j] = j
for i in range(1, len(word1)+1):
for j in range(1, len(word2)+1):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1
return dp[-1][-1]
```

Go:
```Go
Expand Down
64 changes: 64 additions & 0 deletions problems/0106.从中序与后序遍历序列构造二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,70 @@ class Solution:
return root
```
Go:
> 106 从中序与后序遍历序列构造二叉树
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func buildTree(inorder []int, postorder []int) *TreeNode {
if len(inorder)<1||len(postorder)<1{return nil}
//先找到根节点(后续遍历的最后一个就是根节点)
nodeValue:=postorder[len(postorder)-1]
//从中序遍历中找到一分为二的点,左边为左子树,右边为右子树
left:=findRootIndex(inorder,nodeValue)
//构造root
root:=&TreeNode{Val: nodeValue,
Left: buildTree(inorder[:left],postorder[:left]),//将后续遍历一分为二,左边为左子树,右边为右子树
Right: buildTree(inorder[left+1:],postorder[left:len(postorder)-1])}
return root
}
func findRootIndex(inorder []int,target int) (index int){
for i:=0;i<len(inorder);i++{
if target==inorder[i]{
return i
}
}
return -1
}
```

> 105 从前序与中序遍历序列构造二叉树
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func buildTree(preorder []int, inorder []int) *TreeNode {
if len(preorder)<1||len(inorder)<1{return nil}
left:=findRootIndex(preorder[0],inorder)
root:=&TreeNode{
Val: preorder[0],
Left: buildTree(preorder[1:left+1],inorder[:left]),
Right: buildTree(preorder[left+1:],inorder[left+1:])}
return root
}
func findRootIndex(target int,inorder []int) int{
for i:=0;i<len(inorder);i++{
if target==inorder[i]{
return i
}
}
return -1
}
```




JavaScript
Expand Down
Loading

0 comments on commit 5f55278

Please sign in to comment.