diff --git a/assets/avatar/avatar_Gonglja.jpg b/assets/avatar/avatar_Gonglja.jpg new file mode 100644 index 000000000..3bbcc6504 Binary files /dev/null and b/assets/avatar/avatar_Gonglja.jpg differ diff --git a/assets/avatar/avatar_Reanon.jpg b/assets/avatar/avatar_Reanon.jpg new file mode 100644 index 000000000..cd426218f Binary files /dev/null and b/assets/avatar/avatar_Reanon.jpg differ diff --git a/assets/avatar/avatar_codingonion.jpg b/assets/avatar/avatar_codingonion.jpg new file mode 100644 index 000000000..53410ddc4 Binary files /dev/null and b/assets/avatar/avatar_codingonion.jpg differ diff --git a/assets/avatar/avatar_gvenusleo.jpg b/assets/avatar/avatar_gvenusleo.jpg new file mode 100644 index 000000000..40f4dd251 Binary files /dev/null and b/assets/avatar/avatar_gvenusleo.jpg differ diff --git a/assets/avatar/avatar_hpstory.jpg b/assets/avatar/avatar_hpstory.jpg new file mode 100644 index 000000000..47b7b7b6d Binary files /dev/null and b/assets/avatar/avatar_hpstory.jpg differ diff --git a/assets/avatar/avatar_justin-tse.jpg b/assets/avatar/avatar_justin-tse.jpg new file mode 100644 index 000000000..d3260f318 Binary files /dev/null and b/assets/avatar/avatar_justin-tse.jpg differ diff --git a/assets/avatar/avatar_krahets.jpg b/assets/avatar/avatar_krahets.jpg new file mode 100644 index 000000000..9ab8259e7 Binary files /dev/null and b/assets/avatar/avatar_krahets.jpg differ diff --git a/assets/avatar/avatar_night-cruise.jpg b/assets/avatar/avatar_night-cruise.jpg new file mode 100644 index 000000000..4aa9e118e Binary files /dev/null and b/assets/avatar/avatar_night-cruise.jpg differ diff --git a/assets/avatar/avatar_nuomi1.jpg b/assets/avatar/avatar_nuomi1.jpg new file mode 100644 index 000000000..d7d3a67aa Binary files /dev/null and b/assets/avatar/avatar_nuomi1.jpg differ diff --git a/assets/avatar/avatar_yudongjin.jpg b/assets/avatar/avatar_yudongjin.jpg new file mode 100644 index 000000000..64f75e134 Binary files /dev/null and b/assets/avatar/avatar_yudongjin.jpg differ diff --git a/chapter_appendix/terminology/index.html b/chapter_appendix/terminology/index.html index e53b0a1b9..d68b569f2 100644 --- a/chapter_appendix/terminology/index.html +++ b/chapter_appendix/terminology/index.html @@ -3458,345 +3458,369 @@ 算法 algorithm -层序遍历 -level-order traversal +AVL 树 +AVL tree 数据结构 data structure -广度优先遍历 -breadth-first traversal +红黑树 +red-black tree 渐近复杂度分析 asymptotic complexity analysis -深度优先遍历 -depth-first traversal +层序遍历 +level-order traversal 时间复杂度 time complexity -二叉搜索树 -binary search tree +广度优先遍历 +breadth-first traversal 空间复杂度 space complexity -平衡二叉搜索树 -balanced binary search tree +深度优先遍历 +depth-first traversal 迭代 iteration -平衡因子 -balance factor +二叉搜索树 +binary search tree 递归 recursion -堆 -heap +平衡二叉搜索树 +balanced binary search tree 尾递归 tail recursion -大顶堆 -max heap +平衡因子 +balance factor 递归树 recursion tree -小顶堆 -min heap +堆 +heap 大 \(O\) 记号 big-\(O\) notation -优先队列 -priority queue +大顶堆 +max heap 渐近上界 asymptotic upper bound -堆化 -heapify +小顶堆 +min heap 原码 sign-magnitude -图 -graph +优先队列 +priority queue 反码 1’s complement -顶点 -vertex +堆化 +heapify 补码 2’s complement -无向图 -undirected graph +Top-\(k\) 问题 +Top-\(k\) problem 数组 array -有向图 -directed graph +图 +graph 索引 index -连通图 -connected graph +顶点 +vertex 链表 linked list -非连通图 -disconnected graph +无向图 +undirected graph 链表节点 linked list node, list node -有权图 -weighted graph +有向图 +directed graph + + +头节点 +head node +连通图 +connected graph + + +尾节点 +tail node +非连通图 +disconnected graph 列表 list -邻接 -adjacency +有权图 +weighted graph 动态数组 dynamic array -路径 -path +邻接 +adjacency 硬盘 hard disk -入度 -in-degree +路径 +path 内存 random-access memory (RAM) -出度 -out-degree +入度 +in-degree 缓存 cache memory -邻接矩阵 -adjacency matrix +出度 +out-degree 缓存未命中 cache miss -邻接表 -adjacency list +邻接矩阵 +adjacency matrix 缓存命中率 cache hit rate -广度优先搜索 -breadth-first search +邻接表 +adjacency list 栈 stack +广度优先搜索 +breadth-first search + + +栈顶 +top of the stack 深度优先搜索 depth-first search -队列 -queue +栈底 +bottom of the stack 二分查找 binary search -双向队列 -double-ended queue +队列 +queue 搜索算法 searching algorithm -哈希表 -hash table +双向队列 +double-ended queue 排序算法 sorting algorithm -桶 -bucket +队首 +front of the queue 选择排序 selection sort -哈希函数 -hash function +队尾 +rear of the queue 冒泡排序 bubble sort -哈希冲突 -hash collision +哈希表 +hash table 插入排序 insertion sort -负载因子 -load factor +桶 +bucket 快速排序 quick sort -链式地址 -separate chaining +哈希函数 +hash function 归并排序 merge sort -开放寻址 -open addressing +哈希冲突 +hash collision 堆排序 heap sort -线性探测 -linear probing +负载因子 +load factor 桶排序 bucket sort -懒删除 -lazy deletion +链式地址 +separate chaining 计数排序 counting sort -二叉树 -binary tree +开放寻址 +open addressing 基数排序 radix sort -树节点 -tree node +线性探测 +linear probing 分治 divide and conquer -左子节点 -left-child node +懒删除 +lazy deletion 汉诺塔问题 hanota problem -右子节点 -right-child node +二叉树 +binary tree 回溯算法 backtracking algorithm -父节点 -parent node +树节点 +tree node 约束 constraint -左子树 -left subtree +左子节点 +left-child node 解 solution -右子树 -right subtree +右子节点 +right-child node 状态 state -根节点 -root node +父节点 +parent node 剪枝 pruning -叶节点 -leaf node +左子树 +left subtree 全排列问题 permutations problem -边 -edge +右子树 +right subtree 子集和问题 subset-sum problem -层 -level +根节点 +root node n 皇后问题 n-queens problem -度 -degree +叶节点 +leaf node 动态规划 dynamic programming -高度 -height +边 +edge 初始状态 initial state -深度 -depth +层 +level 状态转移方程 state-trasition equation -完美二叉树 -perfect binary tree +度 +degree 背包问题 knapsack problem -完全二叉树 -complete binary tree +高度 +height 编辑距离问题 edit distance problem -完满二叉树 -full binary tree +深度 +depth 贪心算法 greedy algorithm +完美二叉树 +perfect binary tree + + + + +完全二叉树 +complete binary tree + + + + +完满二叉树 +full binary tree + + + + 平衡二叉树 balanced binary tree - -AVL 树 -AVL tree - - - - -红黑树 -red-black tree - - - diff --git a/chapter_preface/index.html b/chapter_preface/index.html index a317b59d4..d6b0fa0d3 100644 --- a/chapter_preface/index.html +++ b/chapter_preface/index.html @@ -3455,9 +3455,9 @@

愿这本书在你的脑海中轻轻响起,留下独特而深刻的旋律。

两年前,我在力扣上分享了“剑指 Offer”系列题解,受到了许多读者的鼓励和支持。在与读者交流期间,我最常被问的一个问题是“如何入门算法”。逐渐地,我对这个问题产生了浓厚的兴趣。

-

两眼一抹黑地刷题似乎是最受欢迎的方法,简单、直接且有效。然而刷题就如同玩“扫雷”游戏,自学能力强的人能够顺利将地雷逐个排掉,而基础不足的人很可能被得的满头是包,并在挫折中步步退缩。通读教材也是一种常见做法,但对于面向求职的人来说,毕业论文、投递简历、准备笔试和面试已经消耗了大部分精力,啃厚重的书往往变成了一项艰巨的挑战。

+

两眼一抹黑地刷题似乎是最受欢迎的方法,简单、直接且有效。然而刷题就如同玩“扫雷”游戏,自学能力强的人能够顺利将地雷逐个排掉,而基础不足的人很可能被炸得满头是包,并在挫折中步步退缩。通读教材也是一种常见做法,但对于面向求职的人来说,毕业论文、投递简历、准备笔试和面试已经消耗了大部分精力,啃厚重的书往往变成了一项艰巨的挑战。

如果你也面临类似的困扰,那么很幸运这本书“找”到了你。本书是我对这个问题给出的答案,即使不是最优解,也至少是一次积极的尝试。本书虽然不足以让你直接拿到 Offer,但会引导你探索数据结构与算法的“知识地图”,带你了解不同“地雷”的形状、大小和分布位置,让你掌握各种“排雷方法”。有了这些本领,相信你可以更加自如地刷题和阅读文献,逐步构建起完整的知识体系。

-

我深深赞同费曼教授所言:“Knowledge isn’t free. You have to pay attention.”从这个意义上看,这本书并非完全“免费”。为了不辜负你为本书所付出的宝贵“注意力”,我会竭尽所能,投入最大的“注意力”来完成本书的创作。

+

我深深赞同费曼教授所言:“Knowledge isn't free. You have to pay attention.”从这个意义上看,这本书并非完全“免费”。为了不辜负你为本书所付出的宝贵“注意力”,我会竭尽所能,投入最大的“注意力”来完成本书的创作。

本章内容

The Figure 3-8 shows the UTF-8 encoding for "Hello算法". It can be observed that since the highest \(n\) bits are set to \(1\), the system can determine the length of the character as \(n\) by counting the number of highest bits set to \(1\).

-

But why set the highest 2 bits of the remaining bytes to \(10\)? Actually, this \(10\) serves as a kind of checksum. If the system starts parsing text from an incorrect byte, the \(10\) at the beginning of the byte can help the system quickly detect an anomaly.

-

The reason for using \(10\) as a checksum is that, under UTF-8 encoding rules, it's impossible for the highest two bits of a character to be \(10\). This can be proven by contradiction: If the highest two bits of a character are \(10\), it indicates that the character's length is \(1\), corresponding to ASCII. However, the highest bit of an ASCII character should be \(0\), contradicting the assumption.

+

But why set the highest 2 bits of the remaining bytes to \(10\)? Actually, this \(10\) serves as a kind of checksum. If the system starts parsing text from an incorrect byte, the \(10\) at the beginning of the byte can help the system quickly detect anomalies.

+

The reason for using \(10\) as a checksum is that, under UTF-8 encoding rules, it's impossible for the highest two bits of a character to be \(10\). This can be proven by contradiction: If the highest two bits of a character are \(10\), it indicates that the character's length is \(1\), corresponding to ASCII. However, the highest bit of an ASCII character should be \(0\), which contradicts the assumption.

UTF-8 Encoding Example

Figure 3-8   UTF-8 Encoding Example

Apart from UTF-8, other common encoding methods include:

-

From the perspective of storage space, UTF-8 is highly efficient for representing English characters, requiring only 1 byte; UTF-16 might be more efficient for encoding some non-English characters (like Chinese), as it requires only 2 bytes, while UTF-8 might need 3 bytes.

-

From a compatibility standpoint, UTF-8 is the most versatile, with many tools and libraries supporting UTF-8 as a priority.

+

From the perspective of storage space, using UTF-8 to represent English characters is very efficient because it only requires 1 byte; using UTF-16 to encode some non-English characters (such as Chinese) can be more efficient because it only requires 2 bytes, while UTF-8 might need 3 bytes.

+

From a compatibility perspective, UTF-8 is the most versatile, with many tools and libraries supporting UTF-8 as a priority.

3.4.5   Character Encoding in Programming Languages

-

In many classic programming languages, strings during program execution are encoded using fixed-length encodings like UTF-16 or UTF-32. This allows strings to be treated as arrays, offering several advantages:

+

Historically, many programming languages utilized fixed-length encodings such as UTF-16 or UTF-32 for processing strings during program execution. This allows strings to be handled as arrays, offering several advantages:

The design of character encoding schemes in programming languages is an interesting topic involving various factors:

-

Due to the underestimation of character counts, these languages had to resort to using "surrogate pairs" to represent Unicode characters exceeding 16 bits. This approach has its drawbacks: strings containing surrogate pairs may have characters occupying 2 or 4 bytes, losing the advantage of fixed-length encoding, and handling surrogate pairs adds to the complexity and debugging difficulty of programming.

-

Owing to these reasons, some programming languages have adopted different encoding schemes:

+

Due to the underestimation of character counts, these languages had to use "surrogate pairs" to represent Unicode characters exceeding 16 bits. This approach has its drawbacks: strings containing surrogate pairs may have characters occupying 2 or 4 bytes, losing the advantage of fixed-length encoding. Additionally, handling surrogate pairs adds complexity and debugging difficulty to programming.

+

Addressing these challenges, some languages have adopted alternative encoding strategies:

-

It’s important to note that the above discussion pertains to how strings are stored in programming languages, which is a different issue from how strings are stored in files or transmitted over networks. For file storage or network transmission, strings are usually encoded in UTF-8 format for optimal compatibility and space efficiency.

+

It’s important to note that the above discussion pertains to how strings are stored in programming languages, which is different from how strings are stored in files or transmitted over networks. For file storage or network transmission, strings are usually encoded in UTF-8 format for optimal compatibility and space efficiency.

diff --git a/en/chapter_stack_and_queue/stack/index.html b/en/chapter_stack_and_queue/stack/index.html index 64e3336de..55d68b52b 100644 --- a/en/chapter_stack_and_queue/stack/index.html +++ b/en/chapter_stack_and_queue/stack/index.html @@ -1620,9 +1620,9 @@

5.1   Stack

-

"Stack" is a linear data structure that follows the principle of Last-In-First-Out (LIFO).

-

We can compare a stack to a pile of plates on a table. To access the bottom plate, one must remove the plates on top. If we replace the plates with various types of elements (such as integers, characters, objects, etc.), we obtain the data structure known as a stack.

-

As shown in the following figure, we refer to the top of the pile of elements as the "top of the stack" and the bottom as the "bottom of the stack." The operation of adding elements to the top of the stack is called "push," and the operation of removing the top element is called "pop."

+

A "Stack" is a linear data structure that follows the principle of Last-In-First-Out (LIFO).

+

We can compare a stack to a pile of plates on a table. To access the bottom plate, one must first remove the plates on top. By replacing the plates with various types of elements (such as integers, characters, objects, etc.), we obtain the data structure known as a stack.

+

As shown in the Figure 5-1 , we refer to the top of the pile of elements as the "top of the stack" and the bottom as the "bottom of the stack." The operation of adding elements to the top of the stack is called "push," and the operation of removing the top element is called "pop."

Stack's Last-In-First-Out Rule

Figure 5-1   Stack's Last-In-First-Out Rule

@@ -1925,8 +1925,8 @@
Full Screen >

5.1.2   Implementing a Stack

-

To understand the mechanics of a stack more deeply, let's try implementing a stack class ourselves.

-

A stack follows the principle of Last-In-First-Out, which means we can only add or remove elements at the top of the stack. However, both arrays and linked lists allow adding and removing elements at any position, therefore a stack can be seen as a restricted array or linked list. In other words, we can "mask" some unrelated operations of arrays or linked lists to make their logic conform to the characteristics of a stack.

+

To gain a deeper understanding of how a stack operates, let's try implementing a stack class ourselves.

+

A stack follows the principle of Last-In-First-Out, which means we can only add or remove elements at the top of the stack. However, both arrays and linked lists allow adding and removing elements at any position, therefore a stack can be seen as a restricted array or linked list. In other words, we can "shield" certain irrelevant operations of an array or linked list, aligning their external behavior with the characteristics of a stack.

1.   Implementation Based on Linked List

When implementing a stack using a linked list, we can consider the head node of the list as the top of the stack and the tail node as the bottom of the stack.

As shown in the Figure 5-2 , for the push operation, we simply insert elements at the head of the linked list. This method of node insertion is known as "head insertion." For the pop operation, we just need to remove the head node from the list.

@@ -3269,7 +3269,7 @@

Supported Operations

Both implementations support all the operations defined in a stack. The array implementation additionally supports random access, but this is beyond the scope of a stack definition and is generally not used.

Time Efficiency

-

In the array-based implementation, both push and pop operations occur in pre-allocated continuous memory, which has good cache locality and therefore higher efficiency. However, if the push operation exceeds the array capacity, it triggers a resizing mechanism, making the time complexity of that push operation \(O(n)\).

+

In the array-based implementation, both push and pop operations occur in pre-allocated contiguous memory, which has good cache locality and therefore higher efficiency. However, if the push operation exceeds the array capacity, it triggers a resizing mechanism, making the time complexity of that push operation \(O(n)\).

In the linked list implementation, list expansion is very flexible, and there is no efficiency decrease issue as in array expansion. However, the push operation requires initializing a node object and modifying pointers, so its efficiency is relatively lower. If the elements being pushed are already node objects, then the initialization step can be skipped, improving efficiency.

Thus, when the elements for push and pop operations are basic data types like int or double, we can draw the following conclusions: