Skip to content

Commit

Permalink
Bug fixes and improvements (#1252)
Browse files Browse the repository at this point in the history
* Bug fixes

* Fix the landing page of zh-hant version

* Bug fixes and add an Q&A for chapter_data_structure

* Update the mindmap for zh-hant version
  • Loading branch information
krahets authored Apr 9, 2024
1 parent 896d9a6 commit 78f71ee
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
4 changes: 2 additions & 2 deletions codes/cpp/chapter_tree/avl_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

/* AVL 树 */
class AVLTree {
public:
TreeNode *root; // 根节点
private:
/* 更新节点高度 */
void updateHeight(TreeNode *node) {
Expand Down Expand Up @@ -134,6 +132,8 @@ class AVLTree {
}

public:
TreeNode *root; // 根节点

/* 获取节点高度 */
int height(TreeNode *node) {
// 空节点高度为 -1 ,叶节点高度为 0
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_dynamic_programming/coin_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func coinChangeDPComp(coins []int, amt int) int {
}
// 状态转移
for i := 1; i <= n; i++ {
// 倒序遍历
// 正序遍历
for a := 1; a <= amt; a++ {
if coins[i-1] > a {
// 若超过目标金额,则不选硬币 i
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_dynamic_programming/coin_change_ii.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func coinChangeIIDPComp(coins []int, amt int) int {
dp[0] = 1
// 状态转移
for i := 1; i <= n; i++ {
// 倒序遍历
// 正序遍历
for a := 1; a <= amt; a++ {
if coins[i-1] > a {
// 若超过目标金额,则不选硬币 i
Expand Down
32 changes: 32 additions & 0 deletions docs/chapter_data_structure/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,35 @@
**Q**:在构建栈(队列)的时候,未指定它的大小,为什么它们是“静态数据结构”呢?

在高级编程语言中,我们无须人工指定栈(队列)的初始容量,这个工作由类内部自动完成。例如,Java 的 `ArrayList` 的初始容量通常为 10。另外,扩容操作也是自动实现的。详见后续的“列表”章节。

**Q**:原码转补码的方法是“先取反后加 1”,那么补码转原码应该是逆运算“先减 1 后取反”,而补码转原码也一样可以通过“先取反后加 1”得到,这是为什么呢?

**A**:这是因为原码和补码的相互转换实际上是计算“补数”的过程。我们先给出补数的定义:假设 $a + b = c$ ,那么我们称 $a$ 是 $b$ 到 $c$ 的补数,反之也称 $b$ 是 $a$ 到 $c$ 的补数。

给定一个 $n = 4$ 位长度的二进制数 $0010$ ,如果将这个数字看作原码(不考虑符号位),那么它的补码需通过“先取反后加 1”得到:

$$
0010 \rightarrow 1101 \rightarrow 1110
$$

我们会发现,原码和补码的和是 $0010 + 1110 = 10000$ ,也就是说,补码 $1110$ 是原码 $0010$ 到 $10000$ 的“补数”。**这意味着上述“先取反后加 1”实际上是计算到 $10000$ 的补数的过程**

那么,补码 $1110$ 到 $10000$ 的“补数”是多少呢?我们依然可以用“先取反后加 1”得到它:

$$
1110 \rightarrow 0001 \rightarrow 0010
$$

换句话说,原码和补码互为对方到 $10000$ 的“补数”,因此“原码转补码”和“补码转原码”可以用相同的操作(先取反后加 1 )实现。

当然,我们也可以用逆运算来求补码 $1110$ 的原码,即“先减 1 后取反”:

$$
1110 \rightarrow 1101 \rightarrow 0010
$$

总结来看,“先取反后加 1”和“先减 1 后取反”这两种运算都是在计算到 $10000$ 的补数,它们是等价的。

本质上看,“取反”操作实际上是求到 $1111$ 的补数(因为恒有 `原码 + 反码 = 1111`);而在反码基础上再加 1 得到的补码,就是到 $10000$ 的补数。

上述 $n = 4$ 为例,其可推广至任意位数的二进制数。
2 changes: 1 addition & 1 deletion zh-hant/docs/chapter_appendix/terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<p align="center"> 表 <id> &nbsp; 資料結構與演算法的重要名詞 </p>

| English | 简体中文 | 繁体中文 |
| English | 簡體中文 | 繁體中文 |
| ------------------------------ | -------------- | -------------- |
| algorithm | 算法 | 演算法 |
| data structure | 数据结构 | 資料結構 |
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions zh-hant/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<!-- heading -->
<div class="heading-div">
<img style="height: min(9vh, 12vw);"
src="https://readme-typing-svg.demolab.com/?font=Noto+Sans+SC&weight=400&duration=3500&pause=2000&color=FFF&center=true&vCenter=true&random=false&width=200&lines=Hello%2C+%E7%AE%97%E6%B3%95+!"
src="https://readme-typing-svg.demolab.com/?font=Noto+Sans+SC&weight=400&duration=3500&pause=2000&color=FFF&center=true&vCenter=true&random=false&width=200&lines=Hello%2C+%E6%BC%94%E7%AE%97%E6%B3%95+!"
alt="" />
<div style="pointer-events: auto;">
<p style="margin-top: max(-1vh, -2vw); margin-bottom: min(2vh, 3.5vw);">
Expand Down Expand Up @@ -140,11 +140,11 @@
<!-- ipad height = 280.6mm -->
<!-- iphone height = 160.7mm -->
<div class="media-block">
<a href="chapter_paperbook/">
<a href="/chapter_paperbook/">
<div style="height: 8.17%;"></div>
<img class="device-on-hover" style="height: 66.83%;" src="../assets/hero/cover_render.png" alt="Cover">
<div class="text-button">
<span>獲取紙質書</span>
<span>紙質書(簡中)</span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
<path
d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z" />
Expand All @@ -170,7 +170,7 @@
<a href="https://github.com/krahets/hello-algo/releases">
<img class="device-on-hover" style="height: 75%;" src="../assets/hero/pdf_ipad.png" alt="">
<div class="text-button">
<span>下載 PDF</span>
<span>PDF(簡中)</span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
<path
d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z" />
Expand Down

0 comments on commit 78f71ee

Please sign in to comment.