mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
Several bug fixes and improvements (#945)
* Update Dockerfile for code debugging. * Format Python code using Black. * Improve dark theme by defining html classes for the figures, animations and cover images. * Fix several glossary translation. * Update a code comment. * Fix climbing_stairs_backtrack: the pruning should not require the sorted choices list. * Update the code of array and list traversal. * Fix a rendering issue of README.md * Update code of list traversal. * Fix array_definition.png * Update README.md * Fix max_capacity_moving_short_board.png * Fix array.dart * Fix array.dart * Fix array.dart * Fix array.dart
This commit is contained in:
@ -2,6 +2,6 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
@ -17,13 +17,13 @@
|
||||
| 递归树 | recursion tree |
|
||||
| 大 $O$ 记号 | big-$O$ notation |
|
||||
| 渐近上界 | asymptotic upper bound |
|
||||
| 原码 | true form |
|
||||
| 反码 | 1's complement code |
|
||||
| 补码 | 2's complement code |
|
||||
| 原码 | sign–magnitude |
|
||||
| 反码 | 1's complement |
|
||||
| 补码 | 2's complement |
|
||||
| 数组 | array |
|
||||
| 索引 | index |
|
||||
| 链表 | linked list |
|
||||
| 链表节点 | linked list node, list node |
|
||||
| 链表节点 | linked list node, list node |
|
||||
| 列表 | list |
|
||||
| 动态数组 | dynamic array |
|
||||
| 栈 | stack |
|
||||
@ -56,8 +56,8 @@
|
||||
| 完全二叉树 | complete binary tree |
|
||||
| 完满二叉树 | full binary tree |
|
||||
| 平衡二叉树 | balanced binary tree |
|
||||
| AVL 树 | AVL tree |
|
||||
| 红黑树 | red-black tree |
|
||||
| AVL 树 | AVL tree |
|
||||
| 红黑树 | red-black tree |
|
||||
| 层序遍历 | level-order traversal |
|
||||
| 广度优先遍历 | breadth-first traversal |
|
||||
| 深度优先遍历 | depth-first traversal |
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 21 KiB |
@ -1,10 +1,6 @@
|
||||
# 数组与链表
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||
|
||||
</div>
|
||||

|
||||
|
||||
!!! abstract
|
||||
|
||||
|
||||
@ -494,12 +494,11 @@
|
||||
# 通过索引遍历列表
|
||||
count = 0
|
||||
for i in range(len(nums)):
|
||||
count += 1
|
||||
count += nums[i]
|
||||
|
||||
# 直接遍历列表元素
|
||||
count = 0
|
||||
for num in nums:
|
||||
count += 1
|
||||
count += num
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@ -508,13 +507,13 @@
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
count += num;
|
||||
}
|
||||
```
|
||||
|
||||
@ -524,13 +523,12 @@
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count++;
|
||||
count += nums.get(i);
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
count += num;
|
||||
}
|
||||
```
|
||||
|
||||
@ -540,13 +538,13 @@
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.Count; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
foreach (int num in nums) {
|
||||
count++;
|
||||
count += num;
|
||||
}
|
||||
```
|
||||
|
||||
@ -556,13 +554,13 @@
|
||||
/* 通过索引遍历列表 */
|
||||
count := 0
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count++
|
||||
count += nums[i]
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0
|
||||
for range nums {
|
||||
count++
|
||||
for _, num := range nums {
|
||||
count += num
|
||||
}
|
||||
```
|
||||
|
||||
@ -571,14 +569,14 @@
|
||||
```swift title="list.swift"
|
||||
/* 通过索引遍历列表 */
|
||||
var count = 0
|
||||
for _ in nums.indices {
|
||||
count += 1
|
||||
for i in nums.indices {
|
||||
count += nums[i]
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0
|
||||
for _ in nums {
|
||||
count += 1
|
||||
for num in nums {
|
||||
count += num
|
||||
}
|
||||
```
|
||||
|
||||
@ -588,13 +586,13 @@
|
||||
/* 通过索引遍历列表 */
|
||||
let count = 0;
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (const num of nums) {
|
||||
count++;
|
||||
count += num;
|
||||
}
|
||||
```
|
||||
|
||||
@ -604,13 +602,13 @@
|
||||
/* 通过索引遍历列表 */
|
||||
let count = 0;
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (const num of nums) {
|
||||
count++;
|
||||
count += num;
|
||||
}
|
||||
```
|
||||
|
||||
@ -619,30 +617,30 @@
|
||||
```dart title="list.dart"
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
count++;
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (int num in nums) {
|
||||
count++;
|
||||
for (var num in nums) {
|
||||
count += num;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="list.rs"
|
||||
/* 通过索引遍历列表 */
|
||||
let mut count = 0;
|
||||
for (index, value) in nums.iter().enumerate() {
|
||||
count += 1;
|
||||
// 通过索引遍历列表
|
||||
let mut _count = 0;
|
||||
for i in 0..nums.len() {
|
||||
_count += nums[i];
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
let mut count = 0;
|
||||
for value in nums.iter() {
|
||||
count += 1;
|
||||
// 直接遍历列表元素
|
||||
_count = 0;
|
||||
for num in &nums {
|
||||
_count += num;
|
||||
}
|
||||
```
|
||||
|
||||
@ -659,13 +657,13 @@
|
||||
var count: i32 = 0;
|
||||
var i: i32 = 0;
|
||||
while (i < nums.items.len) : (i += 1) {
|
||||
count += 1;
|
||||
count += nums[i];
|
||||
}
|
||||
|
||||
// 直接遍历列表元素
|
||||
count = 0;
|
||||
for (nums.items) |_| {
|
||||
count += 1;
|
||||
for (nums.items) |num| {
|
||||
count += num;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@
|
||||
|
||||
不修改 `P.next` 也可以。从该链表的角度看,从头节点遍历到尾节点已经遇不到 `P` 了。这意味着节点 `P` 已经从链表中删除了,此时节点 `P` 指向哪里都不会对这条链表产生影响了。
|
||||
|
||||
从垃圾回收的角度看,对于 Java、Python、Go 等拥有自动垃圾回收的语言来说,节点 `P` 是否被回收取决于是否有仍存在指向它的引用,而不是 `P.next` 的值。在 C 和 C++ 等语言中,我们需要手动释放节点内存。
|
||||
从垃圾回收的角度看,对于 Java、Python、Go 等拥有自动垃圾回收的语言来说,节点 `P` 是否被回收取决于是否仍存在指向它的引用,而不是 `P.next` 的值。在 C 和 C++ 等语言中,我们需要手动释放节点内存。
|
||||
|
||||
!!! question "在链表中插入和删除操作的时间复杂度是 $O(1)$ 。但是增删之前都需要 $O(n)$ 查找元素,那为什么时间复杂度不是 $O(n)$ 呢?"
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
|
||||

|
||||
|
||||
「原码 true form」虽然最直观,但存在一些局限性。一方面,**负数的原码不能直接用于运算**。例如在原码下计算 $1 + (-2)$ ,得到的结果是 $-3$ ,这显然是不对的。
|
||||
「原码 sign–magnitude」虽然最直观,但存在一些局限性。一方面,**负数的原码不能直接用于运算**。例如在原码下计算 $1 + (-2)$ ,得到的结果是 $-3$ ,这显然是不对的。
|
||||
|
||||
$$
|
||||
\begin{aligned}
|
||||
@ -29,7 +29,7 @@ $$
|
||||
\end{aligned}
|
||||
$$
|
||||
|
||||
为了解决此问题,计算机引入了「反码 1's complement code」。如果我们先将原码转换为反码,并在反码下计算 $1 + (-2)$ ,最后将结果从反码转化回原码,则可得到正确结果 $-1$ 。
|
||||
为了解决此问题,计算机引入了「反码 1's complement」。如果我们先将原码转换为反码,并在反码下计算 $1 + (-2)$ ,最后将结果从反码转化回原码,则可得到正确结果 $-1$ 。
|
||||
|
||||
$$
|
||||
\begin{aligned}
|
||||
@ -51,7 +51,7 @@ $$
|
||||
\end{aligned}
|
||||
$$
|
||||
|
||||
与原码一样,反码也存在正负零歧义问题,因此计算机进一步引入了「补码 2's complement code」。我们先来观察一下负零的原码、反码、补码的转换过程:
|
||||
与原码一样,反码也存在正负零歧义问题,因此计算机进一步引入了「补码 2's complement」。我们先来观察一下负零的原码、反码、补码的转换过程:
|
||||
|
||||
$$
|
||||
\begin{aligned}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }
|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -8,8 +8,8 @@ hide:
|
||||
<h1 align="center"> </h1>
|
||||
|
||||
<p align="center">
|
||||
<img src="index.assets/conceptual_rendering.png" width="250">
|
||||
<img src="index.assets/hello_algo_mindmap_tp.png" width="400">
|
||||
<img src="index.assets/conceptual_rendering.png" width="200">
|
||||
<img src="index.assets/hello_algo_mindmap_tp.png" width="320">
|
||||
</p>
|
||||
|
||||
<h2 align="center"> 《 Hello 算法 》</h2>
|
||||
|
||||
Reference in New Issue
Block a user