This commit is contained in:
krahets
2023-11-11 23:03:42 +08:00
parent 0105644232
commit d5a58e0deb
6 changed files with 103 additions and 99 deletions

View File

@ -24,13 +24,13 @@ status: new
| 递归树 | recursion tree |
| 大 $O$ 记号 | big-$O$ notation |
| 渐近上界 | asymptotic upper bound |
| 原码 | true form |
| 反码 | 1's complement code |
| 补码 | 2's complement code |
| 原码 | signmagnitude |
| 反码 | 1's complement |
| 补码 | 2's complement |
| 数组 | array |
| 索引 | index |
| 链表 | linked list |
| 链表节点 | linked list node, list node |
| 链表节点 | linked list node, list node |
| 列表 | list |
| 动态数组 | dynamic array |
| 栈 | stack |
@ -63,8 +63,8 @@ status: new
| 完全二叉树 | 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 |

View File

@ -637,13 +637,14 @@ comments: true
count = 0
# 通过索引遍历数组
for i in range(len(nums)):
count += 1
# 直接遍历数组
count += nums[i]
# 直接遍历数组元素
for num in nums:
count += 1
count += num
# 同时遍历数据索引和元素
for i, num in enumerate(nums):
count += 1
count += nums[i]
count += num
```
=== "C++"
@ -654,7 +655,7 @@ comments: true
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < size; i++) {
count++;
count += nums[i];
}
}
```
@ -667,11 +668,11 @@ comments: true
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < nums.length; i++) {
count++;
count += nums[i];
}
// 直接遍历数组
// 直接遍历数组元素
for (int num : nums) {
count++;
count += num;
}
}
```
@ -684,11 +685,11 @@ comments: true
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < nums.Length; i++) {
count++;
count += nums[i];
}
// 直接遍历数组
// 直接遍历数组元素
foreach (int num in nums) {
count++;
count += num;
}
}
```
@ -701,12 +702,17 @@ comments: true
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
}
// 同时遍历数据索引和元素
for i, num := range nums {
count += nums[i]
count += num
}
}
```
@ -718,12 +724,12 @@ comments: true
func traverse(nums: [Int]) {
var count = 0
// 通过索引遍历数组
for _ in nums.indices {
count += 1
for i in nums.indices {
count += nums[i]
}
// 直接遍历数组
for _ in nums {
count += 1
// 直接遍历数组元素
for num in nums {
count += num
}
}
```
@ -736,11 +742,11 @@ comments: true
let count = 0;
// 通过索引遍历数组
for (let i = 0; i < nums.length; i++) {
count++;
count += nums[i];
}
// 直接遍历数组
// 直接遍历数组元素
for (const num of nums) {
count += 1;
count += num;
}
}
```
@ -753,11 +759,11 @@ comments: true
let count = 0;
// 通过索引遍历数组
for (let i = 0; i < nums.length; i++) {
count++;
count += nums[i];
}
// 直接遍历数组
// 直接遍历数组元素
for (const num of nums) {
count += 1;
count += num;
}
}
```
@ -770,15 +776,15 @@ comments: true
var count = 0;
// 通过索引遍历数组
for (var i = 0; i < nums.length; i++) {
count++;
count += nums[i];
}
// 直接遍历数组
// 直接遍历数组元素
for (var num in nums) {
count++;
count += nums[i];
}
// 通过 forEach 方法遍历数组
nums.forEach((element) {
count++;
count += element;
});
}
```
@ -790,12 +796,12 @@ comments: true
fn traverse(nums: &[i32]) {
let mut _count = 0;
// 通过索引遍历数组
for _ in 0..nums.len() {
_count += 1;
for i in 0..nums.len() {
_count += nums[i];
}
// 直接遍历数组
for _ in nums {
_count += 1;
// 直接遍历数组元素
for num in nums {
_count += num;
}
}
```
@ -808,7 +814,7 @@ comments: true
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < size; i++) {
count++;
count += nums[i];
}
}
```
@ -822,12 +828,12 @@ comments: true
// 通过索引遍历数组
var i: i32 = 0;
while (i < nums.len) : (i += 1) {
count += 1;
count += nums[i];
}
count = 0;
// 直接遍历数组
for (nums) |_| {
count += 1;
// 直接遍历数组元素
for (nums) |num| {
count += num;
}
}
```

View File

@ -498,12 +498,11 @@ comments: true
# 通过索引遍历列表
count = 0
for i in range(len(nums)):
count += 1
count += nums[i]
# 直接遍历列表元素
count = 0
for num in nums:
count += 1
count += num
```
=== "C++"
@ -512,13 +511,13 @@ comments: true
/* 通过索引遍历列表 */
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count++;
count += nums[i];
}
/* 直接遍历列表元素 */
count = 0;
for (int num : nums) {
count++;
count += num;
}
```
@ -528,13 +527,12 @@ comments: true
/* 通过索引遍历列表 */
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;
}
```
@ -544,13 +542,13 @@ comments: true
/* 通过索引遍历列表 */
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;
}
```
@ -560,13 +558,13 @@ comments: true
/* 通过索引遍历列表 */
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
}
```
@ -575,14 +573,14 @@ comments: true
```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
}
```
@ -592,13 +590,13 @@ comments: true
/* 通过索引遍历列表 */
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;
}
```
@ -608,13 +606,13 @@ comments: true
/* 通过索引遍历列表 */
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;
}
```
@ -623,30 +621,30 @@ comments: true
```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;
}
```
@ -663,13 +661,13 @@ comments: true
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;
}
```

View File

@ -37,7 +37,7 @@ comments: true
不修改 `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)$ 呢?"

View File

@ -24,7 +24,7 @@ comments: true
<p align="center"> 图 3-4 &nbsp; 原码、反码与补码之间的相互转换 </p>
「原码 true form」虽然最直观,但存在一些局限性。一方面,**负数的原码不能直接用于运算**。例如在原码下计算 $1 + (-2)$ ,得到的结果是 $-3$ ,这显然是不对的。
「原码 signmagnitude」虽然最直观,但存在一些局限性。一方面,**负数的原码不能直接用于运算**。例如在原码下计算 $1 + (-2)$ ,得到的结果是 $-3$ ,这显然是不对的。
$$
\begin{aligned}
@ -35,7 +35,7 @@ $$
\end{aligned}
$$
为了解决此问题,计算机引入了「反码 1's complement code」。如果我们先将原码转换为反码,并在反码下计算 $1 + (-2)$ ,最后将结果从反码转化回原码,则可得到正确结果 $-1$ 。
为了解决此问题,计算机引入了「反码 1's complement」。如果我们先将原码转换为反码并在反码下计算 $1 + (-2)$ ,最后将结果从反码转化回原码,则可得到正确结果 $-1$ 。
$$
\begin{aligned}
@ -57,7 +57,7 @@ $$
\end{aligned}
$$
与原码一样,反码也存在正负零歧义问题,因此计算机进一步引入了「补码 2's complement code」。我们先来观察一下负零的原码、反码、补码的转换过程:
与原码一样,反码也存在正负零歧义问题,因此计算机进一步引入了「补码 2's complement」。我们先来观察一下负零的原码、反码、补码的转换过程
$$
\begin{aligned}

View File

@ -32,7 +32,7 @@ comments: true
for choice in choices:
# 剪枝:不允许越过第 n 阶
if state + choice > n:
break
continue
# 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res)
# 回退
@ -58,7 +58,7 @@ comments: true
for (auto &choice : choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
break;
continue;
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
@ -87,7 +87,7 @@ comments: true
for (Integer choice : choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
break;
continue;
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
@ -117,7 +117,7 @@ comments: true
foreach (int choice in choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
break;
continue;
// 尝试:做出选择,更新状态
Backtrack(choices, state + choice, n, res);
// 回退
@ -147,7 +147,7 @@ comments: true
for _, choice := range choices {
// 剪枝:不允许越过第 n 阶
if state+choice > n {
break
continue
}
// 尝试:做出选择,更新状态
backtrack(choices, state+choice, n, res)
@ -182,7 +182,7 @@ comments: true
for choice in choices {
// 剪枝:不允许越过第 n 阶
if state + choice > n {
break
continue
}
backtrack(choices: choices, state: state + choice, n: n, res: &res)
}
@ -209,7 +209,7 @@ comments: true
// 遍历所有选择
for (const choice of choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n) break;
if (state + choice > n) continue;
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
@ -242,7 +242,7 @@ comments: true
// 遍历所有选择
for (const choice of choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n) break;
if (state + choice > n) continue;
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
@ -272,7 +272,7 @@ comments: true
// 遍历所有选择
for (int choice in choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n) break;
if (state + choice > n) continue;
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
@ -300,7 +300,7 @@ comments: true
// 遍历所有选择
for &choice in choices {
// 剪枝:不允许越过第 n 阶
if state + choice > n { break; }
if state + choice > n { continue; }
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
@ -331,7 +331,7 @@ comments: true
int choice = choices[i];
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
break;
continue;
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res, len);
// 回退
@ -365,7 +365,7 @@ comments: true
for (choices) |choice| {
// 剪枝:不允许越过第 n 阶
if (state + choice > n) {
break;
continue;
}
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);