Skip to content

Commit

Permalink
Bug fixes and improvements (#1298)
Browse files Browse the repository at this point in the history
* Fix is_empty() implementation in the stack and queue chapter

* Update en/CONTRIBUTING.md

* Remove "剩余" from the state definition of knapsack problem

* Sync zh and zh-hant versions

* Update the stylesheets of code tabs

* Fix quick_sort.rb

* Fix TS code

* Update chapter_paperbook

* Upload the manuscript of 0.1 section

* Fix binary_tree_dfs.rb

* Bug fixes

* Update README

* Update README

* Update README

* Update README.md

* Update README

* Sync zh and zh-hant versions

* Bug fixes
  • Loading branch information
krahets authored Apr 21, 2024
1 parent 74f1a63 commit f616dac
Show file tree
Hide file tree
Showing 61 changed files with 1,607 additions and 146 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
</p>

<p align="center">
<img src="https://www.hello-algo.com/index.assets/animation.gif" width="389">
<img src="https://www.hello-algo.com/index.assets/running_code.gif" width="389">
<img src="https://www.hello-algo.com/index.assets/animation.gif" width="395">
<img src="https://www.hello-algo.com/index.assets/running_code.gif" width="395">
</p>

<p align="center">
<img src="https://img.shields.io/badge/Python-snow?logo=python&logoColor=3776AB">
<img src="https://img.shields.io/badge/C%2B%2B-snow?logo=c%2B%2B&logoColor=00599C">
<img src="https://img.shields.io/badge/Java-snow?logo=coffeescript&logoColor=FC4C02">
<img src="https://img.shields.io/badge/C%23-snow?logo=csharp&logoColor=512BD4">
<img src="https://img.shields.io/badge/Go-snow?logo=go&logoColor=00ADD8">
<img src="https://img.shields.io/badge/Swift-snow?logo=swift&logoColor=F05138">
<img src="https://img.shields.io/badge/JavaScript-snow?logo=javascript&logoColor=E9CE30">
<img src="https://img.shields.io/badge/TypeScript-snow?logo=typescript&logoColor=3178C6">
<img src="https://img.shields.io/badge/Dart-snow?logo=dart&logoColor=0175C2">
<img src="https://img.shields.io/badge/Rust-snow?logo=rust&logoColor=000000">
<img src="https://img.shields.io/badge/C-snow?logo=c&logoColor=A8B9CC">
<img src="https://img.shields.io/badge/Kotlin-snow?logo=kotlin&logoColor=7F52FF">
<img src="https://img.shields.io/badge/Zig-snow?logo=zig&logoColor=F7A41D">
<img src="https://img.shields.io/badge/Python-snow?logo=python&logoColor=3776AB" alt="" />
<img src="https://img.shields.io/badge/Java-snow?logo=coffeescript&logoColor=FC4C02" alt="" />
<img src="https://img.shields.io/badge/C%2B%2B-snow?logo=c%2B%2B&logoColor=00599C" alt="" />
<img src="https://img.shields.io/badge/C-snow?logo=c&logoColor=A8B9CC" alt="" />
<img src="https://img.shields.io/badge/C%23-snow?logo=csharp&logoColor=512BD4" alt="" />
<img src="https://img.shields.io/badge/JavaScript-snow?logo=javascript&logoColor=E9CE30" alt="" />
<img src="https://img.shields.io/badge/Go-snow?logo=go&logoColor=00ADD8" alt="" />
<img src="https://img.shields.io/badge/Swift-snow?logo=swift&logoColor=F05138" alt="" />
<img src="https://img.shields.io/badge/Rust-snow?logo=rust&logoColor=000000" alt="" />
<img src="https://img.shields.io/badge/Ruby-snow?logo=ruby&logoColor=CC342D" alt="" />
<img src="https://img.shields.io/badge/Kotlin-snow?logo=kotlin&logoColor=7F52FF" alt="" />
<img src="https://img.shields.io/badge/TypeScript-snow?logo=typescript&logoColor=3178C6" alt="" />
<img src="https://img.shields.io/badge/Dart-snow?logo=dart&logoColor=0175C2" alt="" />
</p>

<p align="center">
Expand All @@ -51,7 +51,7 @@

- 全书采用动画图解,内容清晰易懂、学习曲线平滑,引导初学者探索数据结构与算法的知识地图。
- 源代码可一键运行,帮助读者在练习中提升编程技能,了解算法工作原理和数据结构底层实现。
- 鼓励读者互助学习,提问与评论通常可在两日内得到回复
- 鼓励读者互助学习,欢迎大家在评论区提出问题、见解和建议

若本书对您有所帮助,请在页面右上角点个 Star :star: 支持一下,谢谢!

Expand Down
2 changes: 1 addition & 1 deletion codes/python/chapter_stack_and_queue/array_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def size(self) -> int:

def is_empty(self) -> bool:
"""判断栈是否为空"""
return self._stack == []
return self.size() == 0

def push(self, item: int):
"""入栈"""
Expand Down
2 changes: 1 addition & 1 deletion codes/python/chapter_stack_and_queue/linkedlist_deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def size(self) -> int:

def is_empty(self) -> bool:
"""判断双向队列是否为空"""
return self.size() == 0
return self._size == 0

def push(self, num: int, is_front: bool):
"""入队操作"""
Expand Down
2 changes: 1 addition & 1 deletion codes/python/chapter_stack_and_queue/linkedlist_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def size(self) -> int:

def is_empty(self) -> bool:
"""判断队列是否为空"""
return not self._front
return self._size == 0

def push(self, num: int):
"""入队"""
Expand Down
2 changes: 1 addition & 1 deletion codes/python/chapter_stack_and_queue/linkedlist_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def size(self) -> int:

def is_empty(self) -> bool:
"""判断栈是否为空"""
return not self._peek
return self._size == 0

def push(self, val: int):
"""入栈"""
Expand Down
3 changes: 2 additions & 1 deletion codes/ruby/chapter_sorting/quick_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,17 @@ def quick_sort(nums, left, right)
end

### Driver Code ###

if __FILE__ == $0
# 快速排序
nums = [2, 4, 1, 0, 3, 5]
QuickSort.quick_sort(nums, 0, nums.length - 1)
puts "快速排序完成后 nums = #{nums}"

# 快速排序(中位基准数优化)
nums1 = [2, 4, 1, 0, 3, 5]
QuickSortMedian.quick_sort(nums1, 0, nums1.length - 1)
puts "快速排序(中位基准数优化)完成后 nums1 = #{nums1}"

# 快速排序(尾递归优化)
nums2 = [2, 4, 1, 0, 3, 5]
QuickSortTailCall.quick_sort(nums2, 0, nums2.length - 1)
Expand Down
8 changes: 4 additions & 4 deletions codes/ruby/chapter_tree/binary_tree_dfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
require_relative '../utils/print_util'

### 前序遍历 ###
def pre_oder(root)
def pre_order(root)
return if root.nil?

# 访问优先级:根节点 -> 左子树 -> 右子树
$res << root.val
pre_oder(root.left)
pre_oder(root.right)
pre_order(root.left)
pre_order(root.right)
end

### 中序遍历 ###
Expand Down Expand Up @@ -47,7 +47,7 @@ def post_order(root)

# 前序遍历
$res = []
pre_oder(root)
pre_order(root)
puts "\n前序遍历的节点打印序列 = #{$res}"

# 中序遍历
Expand Down
2 changes: 1 addition & 1 deletion codes/typescript/chapter_backtracking/subset_sum_i.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ const res = subsetSumI(nums, target);
console.log(`输入数组 nums = ${JSON.stringify(nums)}, target = ${target}`);
console.log(`所有和等于 ${target} 的子集 res = ${JSON.stringify(res)}`);

export { };
export {};
2 changes: 1 addition & 1 deletion codes/typescript/chapter_backtracking/subset_sum_ii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ const res = subsetSumII(nums, target);
console.log(`输入数组 nums = ${JSON.stringify(nums)}, target = ${target}`);
console.log(`所有和等于 ${target} 的子集 res = ${JSON.stringify(res)}`);

export { };
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ console.log(`尾递归函数的求和结果 res = ${res}`);
res = fib(n);
console.log(`斐波那契数列的第 ${n} 项为 ${res}`);

export { };
export {};
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/chapter_divide_and_conquer/divide_and_conquer.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ $$

并行优化在多核或多处理器的环境中尤其有效,因为系统可以同时处理多个子问题,更加充分地利用计算资源,从而显著减少总体的运行时间。

比如在下图所示的“桶排序”中,我们将海量的数据平均分配到各个桶中,则可所有桶的排序任务分散到各个计算单元,完成后再合并结果。
比如在下图所示的“桶排序”中,我们将海量的数据平均分配到各个桶中,则可将所有桶的排序任务分散到各个计算单元,完成后再合并结果。

![桶排序的并行计算](divide_and_conquer.assets/divide_and_conquer_parallel_computing.png)

Expand Down
8 changes: 4 additions & 4 deletions docs/chapter_dynamic_programming/knapsack_problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

**第一步:思考每轮的决策,定义状态,从而得到 $dp$ 表**

对于每个物品来说,不放入背包,背包容量不变;放入背包,背包容量减小。由此可得状态定义:当前物品编号 $i$ 和剩余背包容量 $c$ ,记为 $[i, c]$ 。
对于每个物品来说,不放入背包,背包容量不变;放入背包,背包容量减小。由此可得状态定义:当前物品编号 $i$ 和背包容量 $c$ ,记为 $[i, c]$ 。

状态 $[i, c]$ 对应的子问题为:**前 $i$ 个物品在剩余容量为 $c$ 的背包中的最大价值**,记为 $dp[i, c]$ 。
状态 $[i, c]$ 对应的子问题为:**前 $i$ 个物品在容量为 $c$ 的背包中的最大价值**,记为 $dp[i, c]$ 。

待求解的是 $dp[n, cap]$ ,因此需要一个尺寸为 $(n+1) \times (cap+1)$ 的二维 $dp$ 表。

**第二步:找出最优子结构,进而推导出状态转移方程**

当我们做出物品 $i$ 的决策后,剩余的是前 $i-1$ 个物品的决策,可分为以下两种情况。
当我们做出物品 $i$ 的决策后,剩余的是前 $i-1$ 个物品决策的子问题,可分为以下两种情况。

- **不放入物品 $i$** :背包容量不变,状态变化为 $[i-1, c]$ 。
- **放入物品 $i$** :背包容量减少 $wgt[i-1]$ ,价值增加 $val[i-1]$ ,状态变化为 $[i-1, c-wgt[i-1]]$ 。
Expand All @@ -41,7 +41,7 @@ $$

**第三步:确定边界条件和状态转移顺序**

当无物品或无剩余背包容量时最大价值为 $0$ ,即首列 $dp[i, 0]$ 和首行 $dp[0, c]$ 都等于 $0$ 。
当无物品或背包容量为 $0$ 时最大价值为 $0$ ,即首列 $dp[i, 0]$ 和首行 $dp[0, c]$ 都等于 $0$ 。

当前状态 $[i, c]$ 从上方的状态 $[i-1, c]$ 和左上方的状态 $[i-1, c-wgt[i-1]]$ 转移而来,因此通过两层循环正序遍历整个 $dp$ 表即可。

Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_dynamic_programming/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
**背包问题**

- 背包问题是最典型的动态规划问题之一,具有 0-1 背包、完全背包、多重背包等变种。
- 0-1 背包的状态定义为前 $i$ 个物品在剩余容量为 $c$ 的背包中的最大价值。根据不放入背包和放入背包两种决策,可得到最优子结构,并构建出状态转移方程。在空间优化中,由于每个状态依赖正上方和左上方的状态,因此需要倒序遍历列表,避免左上方状态被覆盖。
- 0-1 背包的状态定义为前 $i$ 个物品在容量为 $c$ 的背包中的最大价值。根据不放入背包和放入背包两种决策,可得到最优子结构,并构建出状态转移方程。在空间优化中,由于每个状态依赖正上方和左上方的状态,因此需要倒序遍历列表,避免左上方状态被覆盖。
- 完全背包问题的每种物品的选取数量无限制,因此选择放入物品的状态转移与 0-1 背包问题不同。由于状态依赖正上方和正左方的状态,因此在空间优化中应当正序遍历。
- 零钱兑换问题是完全背包问题的一个变种。它从求“最大”价值变为求“最小”硬币数量,因此状态转移方程中的 $\max()$ 应改为 $\min()$ 。从追求“不超过”背包容量到追求“恰好”凑出目标金额,因此使用 $amt + 1$ 来表示“无法凑出目标金额”的无效解。
- 零钱兑换问题 II 从求“最少硬币数量”改为求“硬币组合数量”,状态转移方程相应地从 $\min()$ 改为求和运算符。
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_heap/heap.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@

## 堆的实现

下文实现的是大顶堆。若要将其转换为小顶堆,只需将所有大小逻辑判断取逆(例如,将 $\geq$ 替换为 $\leq$ )。感兴趣的读者可以自行实现。
下文实现的是大顶堆。若要将其转换为小顶堆,只需将所有大小逻辑判断进行逆转(例如,将 $\geq$ 替换为 $\leq$ )。感兴趣的读者可以自行实现。

### 堆的存储与表示

Expand Down
7 changes: 4 additions & 3 deletions docs/chapter_paperbook/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ status: new

- 采用全彩印刷,能够原汁原味地发挥出本书“动画图解”的优势。
- 考究纸张材质,既保证色彩高度还原,也保留纸质书特有的质感。
- 纸质版比网页版的格式更加规范,例如图中的公式使用斜体。
- 在不提升定价的前提下,附赠思维导图折页、书签。
- 纸质书、网页版、PDF 版内容同步,随意切换阅读。

!!! tip

由于纸质书和网页版的同步成本较大,因此可能会有一些细节上的不同,请您见谅!
由于纸质书和网页版的同步难度较大,因此可能会有一些细节上的不同,请您见谅!

当然,纸质书也有一些值得大家入手前考虑的地方:

- 使用 Python 语言,可能不匹配你的主语言(也许可以趁此机会练习 Python)。
- 全彩印刷虽然大幅提升了阅读体验,但价格会比黑白印刷高一些。
- 使用 Python 语言,可能不匹配你的主语言(可以把 Python 看作伪代码,重在理解思路)。
- 全彩印刷虽然大幅提升了图解和代码的阅读体验,但价格会比黑白印刷高一些。

!!! tip

Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_sorting/counting_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ $$

## 算法特性

- **时间复杂度为 $O(n + m)$** :涉及遍历 `nums` 和遍历 `counter` ,都使用线性时间。一般情况下 $n \gg m$ ,时间复杂度趋于 $O(n)$ 。
- **时间复杂度为 $O(n + m)$、非自适应排序** :涉及遍历 `nums` 和遍历 `counter` ,都使用线性时间。一般情况下 $n \gg m$ ,时间复杂度趋于 $O(n)$ 。
- **空间复杂度为 $O(n + m)$、非原地排序**:借助了长度分别为 $n$ 和 $m$ 的数组 `res``counter`
- **稳定排序**:由于向 `res` 中填充元素的顺序是“从右向左”的,因此倒序遍历 `nums` 可以避免改变相等元素之间的相对位置,从而实现稳定排序。实际上,正序遍历 `nums` 也可以得到正确的排序结果,但结果是非稳定的。

Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_sorting/radix_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ $$

相较于计数排序,基数排序适用于数值范围较大的情况,**但前提是数据必须可以表示为固定位数的格式,且位数不能过大**。例如,浮点数不适合使用基数排序,因为其位数 $k$ 过大,可能导致时间复杂度 $O(nk) \gg O(n^2)$ 。

- **时间复杂度为 $O(nk)$**:设数据量为 $n$、数据为 $d$ 进制、最大位数为 $k$ ,则对某一位执行计数排序使用 $O(n + d)$ 时间,排序所有 $k$ 位使用 $O((n + d)k)$ 时间。通常情况下,$d$ 和 $k$ 都相对较小,时间复杂度趋向 $O(n)$ 。
- **时间复杂度为 $O(nk)$、非自适应排序**:设数据量为 $n$、数据为 $d$ 进制、最大位数为 $k$ ,则对某一位执行计数排序使用 $O(n + d)$ 时间,排序所有 $k$ 位使用 $O((n + d)k)$ 时间。通常情况下,$d$ 和 $k$ 都相对较小,时间复杂度趋向 $O(n)$ 。
- **空间复杂度为 $O(n + d)$、非原地排序**:与计数排序相同,基数排序需要借助长度为 $n$ 和 $d$ 的数组 `res``counter`
- **稳定排序**:当计数排序稳定时,基数排序也稳定;当计数排序不稳定时,基数排序无法保证得到正确的排序结果。
4 changes: 3 additions & 1 deletion en/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ Don't hesitate to join us via WeChat `krahets-jyd` or on [Discord](https://disco
**Accuracy**:

- Maintain consistency in terminology across translations by referring to the [Terminology](https://www.hello-algo.com/chapter_appendix/terminology/) section.
- Prioritize technical accuracy and maintain the tone and style of the Chinese version. If you think changing the original meaning is necessary, please first submit a PR to modify the Chinese version.
- Prioritize technical accuracy and maintain the tone and style of the Chinese version.
- Always take into account the content and context of the Chinese version to ensure modifications are accurate and comprehensive.

**Authenticity**:

- Translations should flow naturally and fluently, adhering to English expression conventions.
- Always consider the context of the content to harmonize the article.
- Be aware of cultural differences between Chinese and English. For instance, Chinese "pinyin" does not exist in English.
- If the optimized sentence could alter the original meaning, please add a comment for discussion.

**Formatting**:

Expand Down
32 changes: 16 additions & 16 deletions en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
</p>

<p align="center">
<img src="https://www.hello-algo.com/en/index.assets/animation.gif" width="389">
<img src="https://www.hello-algo.com/en/index.assets/running_code.gif" width="389">
<img src="https://www.hello-algo.com/index.assets/animation.gif" width="395">
<img src="https://www.hello-algo.com/index.assets/running_code.gif" width="395">
</p>

<p align="center">
<img src="https://img.shields.io/badge/Python-snow?logo=python&logoColor=3776AB">
<img src="https://img.shields.io/badge/C%2B%2B-snow?logo=c%2B%2B&logoColor=00599C">
<img src="https://img.shields.io/badge/Java-snow?logo=coffeescript&logoColor=FC4C02">
<img src="https://img.shields.io/badge/C%23-snow?logo=csharp&logoColor=512BD4">
<img src="https://img.shields.io/badge/Go-snow?logo=go&logoColor=00ADD8">
<img src="https://img.shields.io/badge/Swift-snow?logo=swift&logoColor=F05138">
<img src="https://img.shields.io/badge/JavaScript-snow?logo=javascript&logoColor=E9CE30">
<img src="https://img.shields.io/badge/TypeScript-snow?logo=typescript&logoColor=3178C6">
<img src="https://img.shields.io/badge/Dart-snow?logo=dart&logoColor=0175C2">
<img src="https://img.shields.io/badge/Rust-snow?logo=rust&logoColor=000000">
<img src="https://img.shields.io/badge/C-snow?logo=c&logoColor=A8B9CC">
<img src="https://img.shields.io/badge/Kotlin-snow?logo=kotlin&logoColor=7F52FF">
<img src="https://img.shields.io/badge/Zig-snow?logo=zig&logoColor=F7A41D">
<img src="https://img.shields.io/badge/Python-snow?logo=python&logoColor=3776AB" alt="" />
<img src="https://img.shields.io/badge/Java-snow?logo=coffeescript&logoColor=FC4C02" alt="" />
<img src="https://img.shields.io/badge/C%2B%2B-snow?logo=c%2B%2B&logoColor=00599C" alt="" />
<img src="https://img.shields.io/badge/C-snow?logo=c&logoColor=A8B9CC" alt="" />
<img src="https://img.shields.io/badge/C%23-snow?logo=csharp&logoColor=512BD4" alt="" />
<img src="https://img.shields.io/badge/JavaScript-snow?logo=javascript&logoColor=E9CE30" alt="" />
<img src="https://img.shields.io/badge/Go-snow?logo=go&logoColor=00ADD8" alt="" />
<img src="https://img.shields.io/badge/Swift-snow?logo=swift&logoColor=F05138" alt="" />
<img src="https://img.shields.io/badge/Rust-snow?logo=rust&logoColor=000000" alt="" />
<img src="https://img.shields.io/badge/Ruby-snow?logo=ruby&logoColor=CC342D" alt="" />
<img src="https://img.shields.io/badge/Kotlin-snow?logo=kotlin&logoColor=7F52FF" alt="" />
<img src="https://img.shields.io/badge/TypeScript-snow?logo=typescript&logoColor=3178C6" alt="" />
<img src="https://img.shields.io/badge/Dart-snow?logo=dart&logoColor=0175C2" alt="" />
</p>

<p align="center">
Expand All @@ -49,7 +49,7 @@ This open-source project aims to create a free and beginner-friendly crash cours

- Animated illustrations, easy-to-understand content, and a smooth learning curve help beginners explore the "knowledge map" of data structures and algorithms.
- Run code with just one click, helping readers improve their programming skills and understand the working principle of algorithms and the underlying implementation of data structures.
- We encourage readers to help each other. Questions and comments are usually replied to within two days.
- Encouraging learning through teaching, feel free to share your questions, insights, and suggestions.

If you find this book helpful, please give it a Star :star: to support us, thank you!

Expand Down
6 changes: 5 additions & 1 deletion overrides/stylesheets/extra.css
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ body {

/* code block tabs */
.md-typeset .tabbed-labels>label {
font-size: 0.545rem;
font-size: 0.61rem;
}

.md-typeset .tabbed-labels--linked>label>a {
padding: .78125em 1.0em .625em;
}

/* header banner */
Expand Down
Loading

0 comments on commit f616dac

Please sign in to comment.