mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:26:27 +08:00
Bug fixes and improvements (#1205)
* Add Ruby code blocks to documents * Remove Ruby code from en/docs * Remove "center-table" class in index.md * Add "data-toc-label" to handle the latex heading during the build process * Use normal JD link instead. * Bug fixes
This commit is contained in:
parent
5ce088de52
commit
b3f100aff1
30 changed files with 27 additions and 115 deletions
|
@ -15,7 +15,7 @@ fn merge(nums: &mut [i32], left: usize, mid: usize, right: usize) {
|
|||
// 当左右子数组都还有元素时,进行比较并将较小的元素复制到临时数组中
|
||||
while i <= mid && j <= right {
|
||||
if nums[i] <= nums[j] {
|
||||
tmp[k] = nums[j];
|
||||
tmp[k] = nums[i];
|
||||
i += 1;
|
||||
} else {
|
||||
tmp[k] = nums[j];
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
# 附录
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![附录](../assets/covers/chapter_appendix.jpg)
|
||||
|
||||
</div>
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 回溯
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![回溯](../assets/covers/chapter_backtracking.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
我们如同迷宫中的探索者,在前进的道路上可能会遇到困难。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 复杂度分析
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![复杂度分析](../assets/covers/chapter_complexity_analysis.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
复杂度分析犹如浩瀚的算法宇宙中的时空向导。
|
||||
|
|
|
@ -790,7 +790,7 @@ $$
|
|||
|
||||
![常见的空间复杂度类型](space_complexity.assets/space_complexity_common_types.png)
|
||||
|
||||
### 常数阶 $O(1)$ {data-toc-label="常数阶"}
|
||||
### 常数阶 $O(1)$
|
||||
|
||||
常数阶常见于数量与输入数据大小 $n$ 无关的常量、变量、对象。
|
||||
|
||||
|
@ -800,7 +800,7 @@ $$
|
|||
[file]{space_complexity}-[class]{}-[func]{constant}
|
||||
```
|
||||
|
||||
### 线性阶 $O(n)$ {data-toc-label="线性阶"}
|
||||
### 线性阶 $O(n)$
|
||||
|
||||
线性阶常见于元素数量与 $n$ 成正比的数组、链表、栈、队列等:
|
||||
|
||||
|
@ -816,7 +816,7 @@ $$
|
|||
|
||||
![递归函数产生的线性阶空间复杂度](space_complexity.assets/space_complexity_recursive_linear.png)
|
||||
|
||||
### 平方阶 $O(n^2)$ {data-toc-label="平方阶"}
|
||||
### 平方阶 $O(n^2)$
|
||||
|
||||
平方阶常见于矩阵和图,元素数量与 $n$ 成平方关系:
|
||||
|
||||
|
@ -832,7 +832,7 @@ $$
|
|||
|
||||
![递归函数产生的平方阶空间复杂度](space_complexity.assets/space_complexity_recursive_quadratic.png)
|
||||
|
||||
### 指数阶 $O(2^n)$ {data-toc-label="指数阶"}
|
||||
### 指数阶 $O(2^n)$
|
||||
|
||||
指数阶常见于二叉树。观察下图,层数为 $n$ 的“满二叉树”的节点数量为 $2^n - 1$ ,占用 $O(2^n)$ 空间:
|
||||
|
||||
|
@ -842,7 +842,7 @@ $$
|
|||
|
||||
![满二叉树产生的指数阶空间复杂度](space_complexity.assets/space_complexity_exponential.png)
|
||||
|
||||
### 对数阶 $O(\log n)$ {data-toc-label="对数阶"}
|
||||
### 对数阶 $O(\log n)$
|
||||
|
||||
对数阶常见于分治算法。例如归并排序,输入长度为 $n$ 的数组,每轮递归将数组从中点处划分为两半,形成高度为 $\log n$ 的递归树,使用 $O(\log n)$ 栈帧空间。
|
||||
|
||||
|
|
|
@ -1033,7 +1033,7 @@ $$
|
|||
|
||||
![常见的时间复杂度类型](time_complexity.assets/time_complexity_common_types.png)
|
||||
|
||||
### 常数阶 $O(1)$ {data-toc-label="常数阶"}
|
||||
### 常数阶 $O(1)$
|
||||
|
||||
常数阶的操作数量与输入数据大小 $n$ 无关,即不随着 $n$ 的变化而变化。
|
||||
|
||||
|
@ -1043,7 +1043,7 @@ $$
|
|||
[file]{time_complexity}-[class]{}-[func]{constant}
|
||||
```
|
||||
|
||||
### 线性阶 $O(n)$ {data-toc-label="线性阶"}
|
||||
### 线性阶 $O(n)$
|
||||
|
||||
线性阶的操作数量相对于输入数据大小 $n$ 以线性级别增长。线性阶通常出现在单层循环中:
|
||||
|
||||
|
@ -1059,7 +1059,7 @@ $$
|
|||
|
||||
值得注意的是,**输入数据大小 $n$ 需根据输入数据的类型来具体确定**。比如在第一个示例中,变量 $n$ 为输入数据大小;在第二个示例中,数组长度 $n$ 为数据大小。
|
||||
|
||||
### 平方阶 $O(n^2)$ {data-toc-label="平方阶"}
|
||||
### 平方阶 $O(n^2)$
|
||||
|
||||
平方阶的操作数量相对于输入数据大小 $n$ 以平方级别增长。平方阶通常出现在嵌套循环中,外层循环和内层循环的时间复杂度都为 $O(n)$ ,因此总体的时间复杂度为 $O(n^2)$ :
|
||||
|
||||
|
@ -1077,7 +1077,7 @@ $$
|
|||
[file]{time_complexity}-[class]{}-[func]{bubble_sort}
|
||||
```
|
||||
|
||||
### 指数阶 $O(2^n)$ {data-toc-label="指数阶"}
|
||||
### 指数阶 $O(2^n)$
|
||||
|
||||
生物学的“细胞分裂”是指数阶增长的典型例子:初始状态为 $1$ 个细胞,分裂一轮后变为 $2$ 个,分裂两轮后变为 $4$ 个,以此类推,分裂 $n$ 轮后有 $2^n$ 个细胞。
|
||||
|
||||
|
@ -1097,7 +1097,7 @@ $$
|
|||
|
||||
指数阶增长非常迅速,在穷举法(暴力搜索、回溯等)中比较常见。对于数据规模较大的问题,指数阶是不可接受的,通常需要使用动态规划或贪心算法等来解决。
|
||||
|
||||
### 对数阶 $O(\log n)$ {data-toc-label="对数阶"}
|
||||
### 对数阶 $O(\log n)$
|
||||
|
||||
与指数阶相反,对数阶反映了“每轮缩减到一半”的情况。设输入数据大小为 $n$ ,由于每轮缩减到一半,因此循环次数是 $\log_2 n$ ,即 $2^n$ 的反函数。
|
||||
|
||||
|
@ -1127,7 +1127,7 @@ $$
|
|||
|
||||
也就是说,底数 $m$ 可以在不影响复杂度的前提下转换。因此我们通常会省略底数 $m$ ,将对数阶直接记为 $O(\log n)$ 。
|
||||
|
||||
### 线性对数阶 $O(n \log n)$ {data-toc-label="线性对数阶"}
|
||||
### 线性对数阶 $O(n \log n)$
|
||||
|
||||
线性对数阶常出现于嵌套循环中,两层循环的时间复杂度分别为 $O(\log n)$ 和 $O(n)$ 。相关代码如下:
|
||||
|
||||
|
@ -1141,7 +1141,7 @@ $$
|
|||
|
||||
主流排序算法的时间复杂度通常为 $O(n \log n)$ ,例如快速排序、归并排序、堆排序等。
|
||||
|
||||
### 阶乘阶 $O(n!)$ {data-toc-label="阶乘阶"}
|
||||
### 阶乘阶 $O(n!)$
|
||||
|
||||
阶乘阶对应数学上的“全排列”问题。给定 $n$ 个互不重复的元素,求其所有可能的排列方案,方案数量为:
|
||||
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 数据结构
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![数据结构](../assets/covers/chapter_data_structure.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
数据结构如同一副稳固而多样的框架。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 分治
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![分治](../assets/covers/chapter_divide_and_conquer.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
难题被逐层拆解,每一次的拆解都使它变得更为简单。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 动态规划
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![动态规划](../assets/covers/chapter_dynamic_programming.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
小溪汇入河流,江河汇入大海。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 图
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![图](../assets/covers/chapter_graph.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
在生命旅途中,我们就像是一个个节点,被无数看不见的边相连。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 贪心
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![贪心](../assets/covers/chapter_greedy.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
向日葵朝着太阳转动,时刻追求自身成长的最大可能。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 哈希表
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![哈希表](../assets/covers/chapter_hashing.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
在计算机世界中,哈希表如同一位聪慧的图书管理员。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 堆
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![堆](../assets/covers/chapter_heap.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
堆就像是山岳峰峦,层叠起伏、形态各异。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 初识算法
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![初识算法](../assets/covers/chapter_introduction.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
一位少女翩翩起舞,与数据交织在一起,裙摆上飘扬着算法的旋律。
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 99 KiB After Width: | Height: | Size: 99 KiB |
|
@ -54,7 +54,7 @@ status: new
|
|||
|
||||
## 购买链接
|
||||
|
||||
如果你对纸质书感兴趣,可以考虑入手一本。我们为大家争取到了新书 5 折优惠,请见[此链接](https://3.cn/-1Wwj1jq)或扫描以下二维码:
|
||||
如果你对纸质书感兴趣,可以考虑入手一本。我们为大家争取到了新书 5 折优惠,请见[此链接](https://3.cn/1X-qmTD3)或扫描以下二维码:
|
||||
|
||||
![](index.assets/book_jd_link.jpg){ class="animation-figure" }
|
||||
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 前言
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![前言](../assets/covers/chapter_preface.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
算法犹如美妙的交响乐,每一行代码都像韵律般流淌。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 搜索
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![搜索](../assets/covers/chapter_searching.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
搜索是一场未知的冒险,我们或许需要走遍神秘空间的每个角落,又或许可以快速锁定目标。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 排序
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![排序](../assets/covers/chapter_sorting.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
排序犹如一把将混乱变为秩序的魔法钥匙,使我们能以更高效的方式理解与处理数据。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 栈与队列
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![栈与队列](../assets/covers/chapter_stack_and_queue.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
栈如同叠猫猫,而队列就像猫猫排队。
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
![完美二叉树的数组表示](array_representation_of_tree.assets/array_representation_binary_tree.png)
|
||||
|
||||
**映射公式的角色相当于链表中的引用**。给定数组中的任意一个节点,我们都可以通过映射公式来访问它的左(右)子节点。
|
||||
**映射公式的角色相当于链表中的节点引用(指针)**。给定数组中的任意一个节点,我们都可以通过映射公式来访问它的左(右)子节点。
|
||||
|
||||
## 表示任意二叉树
|
||||
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# 树
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![树](../assets/covers/chapter_tree.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
参天大树充满生命力,根深叶茂,分枝扶疏。
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# Complexity Analysis
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![complexity_analysis](../assets/covers/chapter_complexity_analysis.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
Complexity analysis is like a space-time navigator in the vast universe of algorithms.
|
||||
|
|
|
@ -736,7 +736,7 @@ $$
|
|||
|
||||
![Common Types of Space Complexity](space_complexity.assets/space_complexity_common_types.png)
|
||||
|
||||
### Constant Order $O(1)$ {data-toc-label="Constant Order"}
|
||||
### Constant Order $O(1)$
|
||||
|
||||
Constant order is common in constants, variables, objects that are independent of the size of input data $n$.
|
||||
|
||||
|
@ -746,7 +746,7 @@ Note that memory occupied by initializing variables or calling functions in a lo
|
|||
[file]{space_complexity}-[class]{}-[func]{constant}
|
||||
```
|
||||
|
||||
### Linear Order $O(n)$ {data-toc-label="Linear Order"}
|
||||
### Linear Order $O(n)$
|
||||
|
||||
Linear order is common in arrays, linked lists, stacks, queues, etc., where the number of elements is proportional to $n$:
|
||||
|
||||
|
@ -762,7 +762,7 @@ As shown below, this function's recursive depth is $n$, meaning there are $n$ in
|
|||
|
||||
![Recursive Function Generating Linear Order Space Complexity](space_complexity.assets/space_complexity_recursive_linear.png)
|
||||
|
||||
### Quadratic Order $O(n^2)$ {data-toc-label="Quadratic Order"}
|
||||
### Quadratic Order $O(n^2)$
|
||||
|
||||
Quadratic order is common in matrices and graphs, where the number of elements is quadratic to $n$:
|
||||
|
||||
|
@ -778,7 +778,7 @@ As shown below, the recursive depth of this function is $n$, and in each recursi
|
|||
|
||||
![Recursive Function Generating Quadratic Order Space Complexity](space_complexity.assets/space_complexity_recursive_quadratic.png)
|
||||
|
||||
### Exponential Order $O(2^n)$ {data-toc-label="Exponential Order"}
|
||||
### Exponential Order $O(2^n)$
|
||||
|
||||
Exponential order is common in binary trees. Observe the below image, a "full binary tree" with $n$ levels has $2^n - 1$ nodes, occupying $O(2^n)$ space:
|
||||
|
||||
|
@ -788,7 +788,7 @@ Exponential order is common in binary trees. Observe the below image, a "full bi
|
|||
|
||||
![Full Binary Tree Generating Exponential Order Space Complexity](space_complexity.assets/space_complexity_exponential.png)
|
||||
|
||||
### Logarithmic Order $O(\log n)$ {data-toc-label="Logarithmic Order"}
|
||||
### Logarithmic Order $O(\log n)$
|
||||
|
||||
Logarithmic order is common in divide-and-conquer algorithms. For example, in merge sort, an array of length $n$ is recursively divided in half each round, forming a recursion tree of height $\log n$, using $O(\log n)$ stack frame space.
|
||||
|
||||
|
|
|
@ -962,7 +962,7 @@ $$
|
|||
|
||||
![Common Types of Time Complexity](time_complexity.assets/time_complexity_common_types.png)
|
||||
|
||||
### Constant Order $O(1)$ {data-toc-label="Constant Order"}
|
||||
### Constant Order $O(1)$
|
||||
|
||||
Constant order means the number of operations is independent of the input data size $n$. In the following function, although the number of operations `size` might be large, the time complexity remains $O(1)$ as it's unrelated to $n$:
|
||||
|
||||
|
@ -970,7 +970,7 @@ Constant order means the number of operations is independent of the input data s
|
|||
[file]{time_complexity}-[class]{}-[func]{constant}
|
||||
```
|
||||
|
||||
### Linear Order $O(n)$ {data-toc-label="Linear Order"}
|
||||
### Linear Order $O(n)$
|
||||
|
||||
Linear order indicates the number of operations grows linearly with the input data size $n$. Linear order commonly appears in single-loop structures:
|
||||
|
||||
|
@ -986,7 +986,7 @@ Operations like array traversal and linked list traversal have a time complexity
|
|||
|
||||
It's important to note that **the input data size $n$ should be determined based on the type of input data**. For example, in the first example, $n$ represents the input data size, while in the second example, the length of the array $n$ is the data size.
|
||||
|
||||
### Quadratic Order $O(n^2)$ {data-toc-label="Quadratic Order"}
|
||||
### Quadratic Order $O(n^2)$
|
||||
|
||||
Quadratic order means the number of operations grows quadratically with the input data size $n$. Quadratic order typically appears in nested loops, where both the outer and inner loops have a time complexity of $O(n)$, resulting in an overall complexity of $O(n^2)$:
|
||||
|
||||
|
@ -1004,7 +1004,7 @@ For instance, in bubble sort, the outer loop runs $n - 1$ times, and the inner l
|
|||
[file]{time_complexity}-[class]{}-[func]{bubble_sort}
|
||||
```
|
||||
|
||||
### Exponential Order $O(2^n)$ {data-toc-label="Exponential Order"}
|
||||
### Exponential Order $O(2^n)$
|
||||
|
||||
Biological "cell division" is a classic example of exponential order growth: starting with one cell, it becomes two after one division, four after two divisions, and so on, resulting in $2^n$ cells after $n$ divisions.
|
||||
|
||||
|
@ -1024,7 +1024,7 @@ In practice, exponential order often appears in recursive functions. For example
|
|||
|
||||
Exponential order growth is extremely rapid and is commonly seen in exhaustive search methods (brute force, backtracking, etc.). For large-scale problems, exponential order is unacceptable, often requiring dynamic programming or greedy algorithms as solutions.
|
||||
|
||||
### Logarithmic Order $O(\log n)$ {data-toc-label="Logarithmic Order"}
|
||||
### Logarithmic Order $O(\log n)$
|
||||
|
||||
In contrast to exponential order, logarithmic order reflects situations where "the size is halved each round." Given an input data size $n$, since the size is halved each round, the number of iterations is $\log_2 n$, the inverse function of $2^n$.
|
||||
|
||||
|
@ -1054,7 +1054,7 @@ Logarithmic order is typical in algorithms based on the divide-and-conquer strat
|
|||
|
||||
This means the base $m$ can be changed without affecting the complexity. Therefore, we often omit the base $m$ and simply denote logarithmic order as $O(\log n)$.
|
||||
|
||||
### Linear-Logarithmic Order $O(n \log n)$ {data-toc-label="Linear-Logarithmic Order"}
|
||||
### Linear-Logarithmic Order $O(n \log n)$
|
||||
|
||||
Linear-logarithmic order often appears in nested loops, with the complexities of the two loops being $O(\log n)$ and $O(n)$ respectively. The related code is as follows:
|
||||
|
||||
|
@ -1068,7 +1068,7 @@ The image below demonstrates how linear-logarithmic order is generated. Each lev
|
|||
|
||||
Mainstream sorting algorithms typically have a time complexity of $O(n \log n)$, such as quicksort, mergesort, and heapsort.
|
||||
|
||||
### Factorial Order $O(n!)$ {data-toc-label="Factorial Order"}
|
||||
### Factorial Order $O(n!)$
|
||||
|
||||
Factorial order corresponds to the mathematical problem of "full permutation." Given $n$ distinct elements, the total number of possible permutations is:
|
||||
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# Data Structures
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![Data Structures](../assets/covers/chapter_data_structure.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
Data structures serve as a robust and diverse framework.
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# Hash Table
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![Hash Table](../assets/covers/chapter_hashing.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
In the world of computing, a hash table is akin to an intelligent librarian.
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# Introduction to Algorithms
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![A first look at the algorithm](../assets/covers/chapter_introduction.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
A graceful maiden dances, intertwined with the data, her skirt swaying to the melody of algorithms.
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# Preface
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![Preface](../assets/covers/chapter_preface.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
Algorithms are like a beautiful symphony, with each line of code flowing like a rhythm.
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
# Stack and Queue
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![Stack and Queue](../assets/covers/chapter_stack_and_queue.jpg)
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
A stack is like cats placed on top of each other, while a queue is like cats lined up one by one.
|
||||
|
|
Loading…
Reference in a new issue