Skip to content

Commit c4a7966

Browse files
authored
Bug fixes and improvements (#1348)
* Add "reference" for EN version. Bug fixes. * Unify the figure reference as "the figure below" and "the figure above". Bug fixes. * Format the EN markdown files. * Replace "" with <u></u> for EN version and bug fixes * Fix biary_tree_dfs.png * Fix biary_tree_dfs.png * Fix zh-hant/biary_tree_dfs.png * Fix heap_sort_step1.png * Sync zh and zh-hant versions. * Bug fixes * Fix EN figures * Bug fixes * Fix the figure labels for EN version
1 parent 8e60d12 commit c4a7966

File tree

99 files changed

+615
-259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+615
-259
lines changed

codes/c/chapter_hashing/simple_hash.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int rotHash(char *key) {
5050

5151
/* Driver Code */
5252
int main() {
53-
char *key = "Hello dsad3241241dsa算123法";
53+
char *key = "Hello 算法";
5454

5555
int hash = addHash(key);
5656
printf("加法哈希值为 %d\n", hash);

codes/cpp/chapter_hashing/simple_hash.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int rotHash(string key) {
4848

4949
/* Driver Code */
5050
int main() {
51-
string key = "Hello dsad3241241dsa算123法";
51+
string key = "Hello 算法";
5252

5353
int hash = addHash(key);
5454
cout << "加法哈希值为 " << hash << endl;

codes/javascript/chapter_dynamic_programming/coin_change.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function coinChangeDP(coins, amt) {
3131
return dp[n][amt] !== MAX ? dp[n][amt] : -1;
3232
}
3333

34-
/* 零钱兑换:状态压缩后的动态规划 */
34+
/* 零钱兑换:空间优化后的动态规划 */
3535
function coinChangeDPComp(coins, amt) {
3636
const n = coins.length;
3737
const MAX = amt + 1;
@@ -61,6 +61,6 @@ const amt = 4;
6161
let res = coinChangeDP(coins, amt);
6262
console.log(`凑到目标金额所需的最少硬币数量为 ${res}`);
6363

64-
// 状态压缩后的动态规划
64+
// 空间优化后的动态规划
6565
res = coinChangeDPComp(coins, amt);
6666
console.log(`凑到目标金额所需的最少硬币数量为 ${res}`);

codes/javascript/chapter_dynamic_programming/coin_change_ii.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function coinChangeIIDP(coins, amt) {
3030
return dp[n][amt];
3131
}
3232

33-
/* 零钱兑换 II:状态压缩后的动态规划 */
33+
/* 零钱兑换 II:空间优化后的动态规划 */
3434
function coinChangeIIDPComp(coins, amt) {
3535
const n = coins.length;
3636
// 初始化 dp 表
@@ -59,6 +59,6 @@ const amt = 5;
5959
let res = coinChangeIIDP(coins, amt);
6060
console.log(`凑出目标金额的硬币组合数量为 ${res}`);
6161

62-
// 状态压缩后的动态规划
62+
// 空间优化后的动态规划
6363
res = coinChangeIIDPComp(coins, amt);
6464
console.log(`凑出目标金额的硬币组合数量为 ${res}`);

codes/javascript/chapter_dynamic_programming/edit_distance.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function editDistanceDP(s, t) {
8282
return dp[n][m];
8383
}
8484

85-
/* 编辑距离:状态压缩后的动态规划 */
85+
/* 编辑距离:空间优化后的动态规划 */
8686
function editDistanceDPComp(s, t) {
8787
const n = s.length,
8888
m = t.length;
@@ -130,6 +130,6 @@ console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);
130130
res = editDistanceDP(s, t);
131131
console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);
132132

133-
// 状态压缩后的动态规划
133+
// 空间优化后的动态规划
134134
res = editDistanceDPComp(s, t);
135135
console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);

codes/javascript/chapter_dynamic_programming/knapsack.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function knapsackDP(wgt, val, cap) {
6969
return dp[n][cap];
7070
}
7171

72-
/* 0-1 背包:状态压缩后的动态规划 */
72+
/* 0-1 背包:空间优化后的动态规划 */
7373
function knapsackDPComp(wgt, val, cap) {
7474
const n = wgt.length;
7575
// 初始化 dp 表
@@ -108,6 +108,6 @@ console.log(`不超过背包容量的最大物品价值为 ${res}`);
108108
res = knapsackDP(wgt, val, cap);
109109
console.log(`不超过背包容量的最大物品价值为 ${res}`);
110110

111-
// 状态压缩后的动态规划
111+
// 空间优化后的动态规划
112112
res = knapsackDPComp(wgt, val, cap);
113113
console.log(`不超过背包容量的最大物品价值为 ${res}`);

codes/javascript/chapter_dynamic_programming/min_cost_climbing_stairs_dp.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function minCostClimbingStairsDP(cost) {
2222
return dp[n];
2323
}
2424

25-
/* 爬楼梯最小代价:状态压缩后的动态规划 */
25+
/* 爬楼梯最小代价:空间优化后的动态规划 */
2626
function minCostClimbingStairsDPComp(cost) {
2727
const n = cost.length - 1;
2828
if (n === 1 || n === 2) {

codes/javascript/chapter_dynamic_programming/min_path_sum.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function minPathSumDP(grid) {
6969
return dp[n - 1][m - 1];
7070
}
7171

72-
/* 最小路径和:状态压缩后的动态规划 */
72+
/* 最小路径和:空间优化后的动态规划 */
7373
function minPathSumDPComp(grid) {
7474
const n = grid.length,
7575
m = grid[0].length;
@@ -116,6 +116,6 @@ console.log(`从左上角到右下角的最小路径和为 ${res}`);
116116
res = minPathSumDP(grid);
117117
console.log(`从左上角到右下角的最小路径和为 ${res}`);
118118

119-
// 状态压缩后的动态规划
119+
// 空间优化后的动态规划
120120
res = minPathSumDPComp(grid);
121121
console.log(`从左上角到右下角的最小路径和为 ${res}`);

codes/javascript/chapter_dynamic_programming/unbounded_knapsack.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function unboundedKnapsackDP(wgt, val, cap) {
2929
return dp[n][cap];
3030
}
3131

32-
/* 完全背包:状态压缩后的动态规划 */
32+
/* 完全背包:空间优化后的动态规划 */
3333
function unboundedKnapsackDPComp(wgt, val, cap) {
3434
const n = wgt.length;
3535
// 初始化 dp 表
@@ -58,6 +58,6 @@ const cap = 4;
5858
let res = unboundedKnapsackDP(wgt, val, cap);
5959
console.log(`不超过背包容量的最大物品价值为 ${res}`);
6060

61-
// 状态压缩后的动态规划
61+
// 空间优化后的动态规划
6262
res = unboundedKnapsackDPComp(wgt, val, cap);
6363
console.log(`不超过背包容量的最大物品价值为 ${res}`);

codes/ruby/chapter_sorting/quick_sort.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class QuickSort
99
class << self
1010
### 哨兵划分 ###
1111
def partition(nums, left, right)
12-
1312
# 以 nums[left] 为基准数
1413
i, j = left, right
1514
while i < j
@@ -116,7 +115,7 @@ def partition(nums, left, right)
116115
i # 返回基准数的索引
117116
end
118117

119-
### 快速排序(尾递归优化)
118+
### 快速排序(尾递归优化)###
120119
def quick_sort(nums, left, right)
121120
# 子数组长度不为 1 时递归
122121
while left < right

codes/typescript/chapter_dynamic_programming/coin_change.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function coinChangeDP(coins: Array<number>, amt: number): number {
3131
return dp[n][amt] !== MAX ? dp[n][amt] : -1;
3232
}
3333

34-
/* 零钱兑换:状态压缩后的动态规划 */
34+
/* 零钱兑换:空间优化后的动态规划 */
3535
function coinChangeDPComp(coins: Array<number>, amt: number): number {
3636
const n = coins.length;
3737
const MAX = amt + 1;
@@ -61,7 +61,7 @@ const amt = 4;
6161
let res = coinChangeDP(coins, amt);
6262
console.log(`凑到目标金额所需的最少硬币数量为 ${res}`);
6363

64-
// 状态压缩后的动态规划
64+
// 空间优化后的动态规划
6565
res = coinChangeDPComp(coins, amt);
6666
console.log(`凑到目标金额所需的最少硬币数量为 ${res}`);
6767

codes/typescript/chapter_dynamic_programming/coin_change_ii.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function coinChangeIIDP(coins: Array<number>, amt: number): number {
3030
return dp[n][amt];
3131
}
3232

33-
/* 零钱兑换 II:状态压缩后的动态规划 */
33+
/* 零钱兑换 II:空间优化后的动态规划 */
3434
function coinChangeIIDPComp(coins: Array<number>, amt: number): number {
3535
const n = coins.length;
3636
// 初始化 dp 表
@@ -59,7 +59,7 @@ const amt = 5;
5959
let res = coinChangeIIDP(coins, amt);
6060
console.log(`凑出目标金额的硬币组合数量为 ${res}`);
6161

62-
// 状态压缩后的动态规划
62+
// 空间优化后的动态规划
6363
res = coinChangeIIDPComp(coins, amt);
6464
console.log(`凑出目标金额的硬币组合数量为 ${res}`);
6565

codes/typescript/chapter_dynamic_programming/edit_distance.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function editDistanceDP(s: string, t: string): number {
9090
return dp[n][m];
9191
}
9292

93-
/* 编辑距离:状态压缩后的动态规划 */
93+
/* 编辑距离:空间优化后的动态规划 */
9494
function editDistanceDPComp(s: string, t: string): number {
9595
const n = s.length,
9696
m = t.length;
@@ -141,7 +141,7 @@ console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);
141141
res = editDistanceDP(s, t);
142142
console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);
143143

144-
// 状态压缩后的动态规划
144+
// 空间优化后的动态规划
145145
res = editDistanceDPComp(s, t);
146146
console.log(`将 ${s} 更改为 ${t} 最少需要编辑 ${res} 步`);
147147

codes/typescript/chapter_dynamic_programming/knapsack.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function knapsackDP(
8484
return dp[n][cap];
8585
}
8686

87-
/* 0-1 背包:状态压缩后的动态规划 */
87+
/* 0-1 背包:空间优化后的动态规划 */
8888
function knapsackDPComp(
8989
wgt: Array<number>,
9090
val: Array<number>,
@@ -127,7 +127,7 @@ console.log(`不超过背包容量的最大物品价值为 ${res}`);
127127
res = knapsackDP(wgt, val, cap);
128128
console.log(`不超过背包容量的最大物品价值为 ${res}`);
129129

130-
// 状态压缩后的动态规划
130+
// 空间优化后的动态规划
131131
res = knapsackDPComp(wgt, val, cap);
132132
console.log(`不超过背包容量的最大物品价值为 ${res}`);
133133

codes/typescript/chapter_dynamic_programming/min_cost_climbing_stairs_dp.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function minCostClimbingStairsDP(cost: Array<number>): number {
2222
return dp[n];
2323
}
2424

25-
/* 爬楼梯最小代价:状态压缩后的动态规划 */
25+
/* 爬楼梯最小代价:空间优化后的动态规划 */
2626
function minCostClimbingStairsDPComp(cost: Array<number>): number {
2727
const n = cost.length - 1;
2828
if (n === 1 || n === 2) {

codes/typescript/chapter_dynamic_programming/min_path_sum.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function minPathSumDP(grid: Array<Array<number>>): number {
7878
return dp[n - 1][m - 1];
7979
}
8080

81-
/* 最小路径和:状态压缩后的动态规划 */
81+
/* 最小路径和:空间优化后的动态规划 */
8282
function minPathSumDPComp(grid: Array<Array<number>>): number {
8383
const n = grid.length,
8484
m = grid[0].length;
@@ -125,7 +125,7 @@ console.log(`从左上角到右下角的最小路径和为 ${res}`);
125125
res = minPathSumDP(grid);
126126
console.log(`从左上角到右下角的最小路径和为 ${res}`);
127127

128-
// 状态压缩后的动态规划
128+
// 空间优化后的动态规划
129129
res = minPathSumDPComp(grid);
130130
console.log(`从左上角到右下角的最小路径和为 ${res}`);
131131

codes/typescript/chapter_dynamic_programming/unbounded_knapsack.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function unboundedKnapsackDP(
3333
return dp[n][cap];
3434
}
3535

36-
/* 完全背包:状态压缩后的动态规划 */
36+
/* 完全背包:空间优化后的动态规划 */
3737
function unboundedKnapsackDPComp(
3838
wgt: Array<number>,
3939
val: Array<number>,
@@ -66,7 +66,7 @@ const cap = 4;
6666
let res = unboundedKnapsackDP(wgt, val, cap);
6767
console.log(`不超过背包容量的最大物品价值为 ${res}`);
6868

69-
// 状态压缩后的动态规划
69+
// 空间优化后的动态规划
7070
res = unboundedKnapsackDPComp(wgt, val, cap);
7171
console.log(`不超过背包容量的最大物品价值为 ${res}`);
7272

docs/chapter_backtracking/n_queens_problem.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
也就是说,我们可以采取逐行放置策略:从第一行开始,在每行放置一个皇后,直至最后一行结束。
2020

21-
下图所示为 $4$ 皇后问题的逐行放置过程。受画幅限制,下图仅展开了第一行的其中一个搜索分支,并且将不满足列约束和对角线约束的方案都进行了剪枝。
21+
下图所示为 4 皇后问题的逐行放置过程。受画幅限制,下图仅展开了第一行的其中一个搜索分支,并且将不满足列约束和对角线约束的方案都进行了剪枝。
2222

2323
![逐行放置策略](n_queens_problem.assets/n_queens_placing.png)
2424

Loading
Loading
Loading

en/docs/chapter_appendix/contribution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ As shown in the figure below, there is an "edit icon" in the upper right corner
2020

2121
![Edit page button](contribution.assets/edit_markdown.png)
2222

23-
Images cannot be directly modified and require the creation of a new [Issue](https://github.com/krahets/hello-algo/issues) or a comment to describe the problem. We will redraw and replace the images as soon as possible.
23+
Figures cannot be directly modified and require the creation of a new [Issue](https://github.com/krahets/hello-algo/issues) or a comment to describe the problem. We will redraw and replace the figures as soon as possible.
2424

2525
### Content creation
2626

Loading
Loading

en/docs/chapter_appendix/installation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ We recommend using the open-source, lightweight VS Code as your local Integrated
66

77
![Download VS Code from the official website](installation.assets/vscode_installation.png)
88

9-
VS Code has a powerful extension ecosystem, supporting the execution and debugging of most programming languages. For example, after installing the "Python Extension Pack," you can debug Python code. The installation steps are shown in the following figure.
9+
VS Code has a powerful extension ecosystem, supporting the execution and debugging of most programming languages. For example, after installing the "Python Extension Pack," you can debug Python code. The installation steps are shown in the figure below.
1010

1111
![Install VS Code Extension Pack](installation.assets/vscode_extension_installation.png)
1212

en/docs/chapter_array_and_linkedlist/array.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Array
22

3-
An "array" is a linear data structure that operates as a lineup of similar items, stored together in a computer's memory in contiguous spaces. It's like a sequence that maintains organized storage. Each item in this lineup has its unique 'spot' known as an "index". Please refer to the figure below to observe how arrays work and grasp these key terms.
3+
An <u>array</u> is a linear data structure that operates as a lineup of similar items, stored together in a computer's memory in contiguous spaces. It's like a sequence that maintains organized storage. Each item in this lineup has its unique 'spot' known as an <u>index</u>. Please refer to the figure below to observe how arrays work and grasp these key terms.
44

55
![Array definition and storage method](array.assets/array_definition.png)
66

@@ -125,7 +125,7 @@ Elements in an array are stored in contiguous memory spaces, making it simpler t
125125

126126
![Memory address calculation for array elements](array.assets/array_memory_location_calculation.png)
127127

128-
As observed in the above illustration, array indexing conventionally begins at $0$. While this might appear counterintuitive, considering counting usually starts at $1$, within the address calculation formula, **an index is essentially an offset from the memory address**. For the first element's address, this offset is $0$, validating its index as $0$.
128+
As observed in the figure above, array indexing conventionally begins at $0$. While this might appear counterintuitive, considering counting usually starts at $1$, within the address calculation formula, **an index is essentially an offset from the memory address**. For the first element's address, this offset is $0$, validating its index as $0$.
129129

130130
Accessing elements in an array is highly efficient, allowing us to randomly access any element in $O(1)$ time.
131131

@@ -135,7 +135,7 @@ Accessing elements in an array is highly efficient, allowing us to randomly acce
135135

136136
### Inserting elements
137137

138-
Array elements are tightly packed in memory, with no space available to accommodate additional data between them. Illustrated in Figure below, inserting an element in the middle of an array requires shifting all subsequent elements back by one position to create room for the new element.
138+
Array elements are tightly packed in memory, with no space available to accommodate additional data between them. As illustrated in the figure below, inserting an element in the middle of an array requires shifting all subsequent elements back by one position to create room for the new element.
139139

140140
![Array element insertion example](array.assets/array_insert_element.png)
141141

en/docs/chapter_array_and_linkedlist/linked_list.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
Memory space is a shared resource among all programs. In a complex system environment, available memory can be dispersed throughout the memory space. We understand that the memory allocated for an array must be continuous. However, for very large arrays, finding a sufficiently large contiguous memory space might be challenging. This is where the flexible advantage of linked lists becomes evident.
44

5-
A "linked list" is a linear data structure in which each element is a node object, and the nodes are interconnected through "references". These references hold the memory addresses of subsequent nodes, enabling navigation from one node to the next.
5+
A <u>linked list</u> is a linear data structure in which each element is a node object, and the nodes are interconnected through "references". These references hold the memory addresses of subsequent nodes, enabling navigation from one node to the next.
66

77
The design of linked lists allows for their nodes to be distributed across memory locations without requiring contiguous memory addresses.
88

99
![Linked list definition and storage method](linked_list.assets/linkedlist_definition.png)
1010

11-
As shown in the figure, we see that the basic building block of a linked list is the "node" object. Each node comprises two key components: the node's "value" and a "reference" to the next node.
11+
As shown in the figure above, we see that the basic building block of a linked list is the <u>node</u> object. Each node comprises two key components: the node's "value" and a "reference" to the next node.
1212

1313
- The first node in a linked list is the "head node", and the final one is the "tail node".
1414
- The tail node points to "null", designated as `null` in Java, `nullptr` in C++, and `None` in Python.
@@ -406,7 +406,7 @@ The array as a whole is a variable, for instance, the array `nums` includes elem
406406

407407
### Inserting nodes
408408

409-
Inserting a node into a linked list is very easy. As shown in the figure, let's assume we aim to insert a new node `P` between two adjacent nodes `n0` and `n1`. **This can be achieved by simply modifying two node references (pointers)**, with a time complexity of $O(1)$.
409+
Inserting a node into a linked list is very easy. As shown in the figure below, let's assume we aim to insert a new node `P` between two adjacent nodes `n0` and `n1`. **This can be achieved by simply modifying two node references (pointers)**, with a time complexity of $O(1)$.
410410

411411
By comparison, inserting an element into an array has a time complexity of $O(n)$, which becomes less efficient when dealing with large data volumes.
412412

@@ -418,7 +418,7 @@ By comparison, inserting an element into an array has a time complexity of $O(n)
418418

419419
### Deleting nodes
420420

421-
As shown in the figure, deleting a node from a linked list is also very easy, **involving only the modification of a single node's reference (pointer)**.
421+
As shown in the figure below, deleting a node from a linked list is also very easy, **involving only the modification of a single node's reference (pointer)**.
422422

423423
It's important to note that even though node `P` continues to point to `n1` after being deleted, it becomes inaccessible during linked list traversal. This effectively means that `P` is no longer a part of the linked list.
424424

@@ -461,7 +461,7 @@ The table below summarizes the characteristics of arrays and linked lists, and i
461461

462462
## Common types of linked lists
463463

464-
As shown in the figure, there are three common types of linked lists.
464+
As shown in the figure below, there are three common types of linked lists.
465465

466466
- **Singly linked list**: This is the standard linked list described earlier. Nodes in a singly linked list include a value and a reference to the next node. The first node is known as the head node, and the last node, which points to null (`None`), is the tail node.
467467
- **Circular linked list**: This is formed when the tail node of a singly linked list points back to the head node, creating a loop. In a circular linked list, any node can function as the head node.

en/docs/chapter_array_and_linkedlist/list.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# List
22

3-
A "list" is an abstract data structure concept that represents an ordered collection of elements, supporting operations such as element access, modification, addition, deletion, and traversal, without requiring users to consider capacity limitations. Lists can be implemented based on linked lists or arrays.
3+
A <u>list</u> is an abstract data structure concept that represents an ordered collection of elements, supporting operations such as element access, modification, addition, deletion, and traversal, without requiring users to consider capacity limitations. Lists can be implemented based on linked lists or arrays.
44

55
- A linked list inherently serves as a list, supporting operations for adding, deleting, searching, and modifying elements, with the flexibility to dynamically adjust its size.
66
- Arrays also support these operations, but due to their immutable length, they can be considered as a list with a length limit.
77

88
When implementing lists using arrays, **the immutability of length reduces the practicality of the list**. This is because predicting the amount of data to be stored in advance is often challenging, making it difficult to choose an appropriate list length. If the length is too small, it may not meet the requirements; if too large, it may waste memory space.
99

10-
To solve this problem, we can implement lists using a "dynamic array." It inherits the advantages of arrays and can dynamically expand during program execution.
10+
To solve this problem, we can implement lists using a <u>dynamic array</u>. It inherits the advantages of arrays and can dynamically expand during program execution.
1111

1212
In fact, **many programming languages' standard libraries implement lists using dynamic arrays**, such as Python's `list`, Java's `ArrayList`, C++'s `vector`, and C#'s `List`. In the following discussion, we will consider "list" and "dynamic array" as synonymous concepts.
1313

0 commit comments

Comments
 (0)