diff --git a/README.md b/README.md index 600ab6518..40b1d915e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@

- - +

@@ -12,11 +11,9 @@

- - + - - +

@@ -66,7 +63,7 @@ - [内容修正](https://www.hello-algo.com/chapter_appendix/contribution/):请您协助修正或在评论区指出语法错误、内容缺失、文字歧义、无效链接或代码 bug 等问题。 - [代码转译](https://github.com/krahets/hello-algo/issues/15):期待您贡献各种语言代码,已支持 Python、Java、C++、Go、JavaScript 等 12 门编程语言。 -- [整书翻译](https://github.com/krahets/hello-algo/tree/en):诚邀您加入我们的中译英小组,成员主要来自计算机相关专业、英语专业和英文母语者。 +- [Chinese-to-English](https://github.com/krahets/hello-algo/tree/en):诚邀您加入我们的翻译小组,成员主要来自计算机相关专业、英语专业和英文母语者。 欢迎您提出宝贵意见和建议,如有任何问题请提交 Issues 或微信联系 `krahets-jyd` 。 diff --git a/codes/Dockerfile b/codes/Dockerfile index 1e4a57523..310d91cb7 100644 --- a/codes/Dockerfile +++ b/codes/Dockerfile @@ -10,7 +10,7 @@ RUN apt-get update && apt-get install -y wget # Install languages environment ARG LANGS RUN for LANG in $LANGS; do \ - case "$LANG" in \ + case $LANG in \ python) \ apt-get install -y python3.10 && \ update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1 ;; \ diff --git a/codes/c/chapter_array_and_linkedlist/array.c b/codes/c/chapter_array_and_linkedlist/array.c index 11f6fa0b1..217d0cb0c 100644 --- a/codes/c/chapter_array_and_linkedlist/array.c +++ b/codes/c/chapter_array_and_linkedlist/array.c @@ -55,7 +55,7 @@ void traverse(int *nums, int size) { int count = 0; // 通过索引遍历数组 for (int i = 0; i < size; i++) { - count++; + count += nums[i]; } } diff --git a/codes/c/chapter_dynamic_programming/climbing_stairs_backtrack.c b/codes/c/chapter_dynamic_programming/climbing_stairs_backtrack.c index 7106cd267..5e5b3eba2 100644 --- a/codes/c/chapter_dynamic_programming/climbing_stairs_backtrack.c +++ b/codes/c/chapter_dynamic_programming/climbing_stairs_backtrack.c @@ -16,7 +16,7 @@ void backtrack(int *choices, int state, int n, int *res, int len) { int choice = choices[i]; // 剪枝:不允许越过第 n 阶 if (state + choice > n) - break; + continue; // 尝试:做出选择,更新状态 backtrack(choices, state + choice, n, res, len); // 回退 diff --git a/codes/cpp/chapter_array_and_linkedlist/array.cpp b/codes/cpp/chapter_array_and_linkedlist/array.cpp index c3ba85705..e3507840d 100644 --- a/codes/cpp/chapter_array_and_linkedlist/array.cpp +++ b/codes/cpp/chapter_array_and_linkedlist/array.cpp @@ -52,7 +52,7 @@ void traverse(int *nums, int size) { int count = 0; // 通过索引遍历数组 for (int i = 0; i < size; i++) { - count++; + count += nums[i]; } } diff --git a/codes/cpp/chapter_array_and_linkedlist/list.cpp b/codes/cpp/chapter_array_and_linkedlist/list.cpp index 558d08e11..cd10c0ca1 100644 --- a/codes/cpp/chapter_array_and_linkedlist/list.cpp +++ b/codes/cpp/chapter_array_and_linkedlist/list.cpp @@ -14,8 +14,8 @@ int main() { printVector(nums); /* 访问元素 */ - int x = nums[1]; - cout << "访问索引 1 处的元素,得到 x = " << x << endl; + int num = nums[1]; + cout << "访问索引 1 处的元素,得到 num = " << num << endl; /* 更新元素 */ nums[1] = 0; @@ -49,13 +49,12 @@ int main() { /* 通过索引遍历列表 */ int count = 0; for (int i = 0; i < nums.size(); i++) { - count++; + count += nums[i]; } - /* 直接遍历列表元素 */ count = 0; - for (int n : nums) { - count++; + for (int x : nums) { + count += x; } /* 拼接两个列表 */ diff --git a/codes/cpp/chapter_dynamic_programming/climbing_stairs_backtrack.cpp b/codes/cpp/chapter_dynamic_programming/climbing_stairs_backtrack.cpp index 167065109..7e7e352b1 100644 --- a/codes/cpp/chapter_dynamic_programming/climbing_stairs_backtrack.cpp +++ b/codes/cpp/chapter_dynamic_programming/climbing_stairs_backtrack.cpp @@ -16,7 +16,7 @@ void backtrack(vector &choices, int state, int n, vector &res) { for (auto &choice : choices) { // 剪枝:不允许越过第 n 阶 if (state + choice > n) - break; + continue; // 尝试:做出选择,更新状态 backtrack(choices, state + choice, n, res); // 回退 diff --git a/codes/csharp/chapter_array_and_linkedlist/array.cs b/codes/csharp/chapter_array_and_linkedlist/array.cs index fe5f60e73..dfab3a051 100644 --- a/codes/csharp/chapter_array_and_linkedlist/array.cs +++ b/codes/csharp/chapter_array_and_linkedlist/array.cs @@ -50,11 +50,11 @@ public class array { int count = 0; // 通过索引遍历数组 for (int i = 0; i < nums.Length; i++) { - count++; + count += nums[i]; } - // 直接遍历数组 + // 直接遍历数组元素 foreach (int num in nums) { - count++; + count += num; } } diff --git a/codes/csharp/chapter_array_and_linkedlist/list.cs b/codes/csharp/chapter_array_and_linkedlist/list.cs index 4dea8a70b..72f3687bb 100644 --- a/codes/csharp/chapter_array_and_linkedlist/list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/list.cs @@ -46,13 +46,12 @@ public class list { /* 通过索引遍历列表 */ int count = 0; for (int i = 0; i < nums.Count; i++) { - count++; + count += nums[i]; } - /* 直接遍历列表元素 */ count = 0; - foreach (int n in nums) { - count++; + foreach (int x in nums) { + count += x; } /* 拼接两个列表 */ diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs index a4ae85706..3cbae9688 100644 --- a/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs @@ -16,7 +16,7 @@ public class climbing_stairs_backtrack { foreach (int choice in choices) { // 剪枝:不允许越过第 n 阶 if (state + choice > n) - break; + continue; // 尝试:做出选择,更新状态 Backtrack(choices, state + choice, n, res); // 回退 diff --git a/codes/dart/chapter_array_and_linkedlist/array.dart b/codes/dart/chapter_array_and_linkedlist/array.dart index 9309d64f4..2e4fc4b00 100644 --- a/codes/dart/chapter_array_and_linkedlist/array.dart +++ b/codes/dart/chapter_array_and_linkedlist/array.dart @@ -9,7 +9,7 @@ import 'dart:math'; /* 随机访问元素 */ -int randomAccess(List nums) { +int randomAccess(List nums) { // 在区间 [0, nums.length) 中随机抽取一个数字 int randomIndex = Random().nextInt(nums.length); // 获取并返回随机元素 @@ -18,7 +18,7 @@ int randomAccess(List nums) { } /* 扩展数组长度 */ -List extend(List nums, int enlarge) { +List extend(List nums, int enlarge) { // 初始化一个扩展长度后的数组 List res = List.filled(nums.length + enlarge, 0); // 将原数组中的所有元素复制到新数组 @@ -30,7 +30,7 @@ List extend(List nums, int enlarge) { } /* 在数组的索引 index 处插入元素 num */ -void insert(List nums, int num, int index) { +void insert(List nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 for (var i = nums.length - 1; i > index; i--) { nums[i] = nums[i - 1]; @@ -40,7 +40,7 @@ void insert(List nums, int num, int index) { } /* 删除索引 index 处元素 */ -void remove(List nums, int index) { +void remove(List nums, int index) { // 把索引 index 之后的所有元素向前移动一位 for (var i = index; i < nums.length - 1; i++) { nums[i] = nums[i + 1]; @@ -48,24 +48,24 @@ void remove(List nums, int index) { } /* 遍历数组元素 */ -void traverse(List nums) { - var count = 0; +void traverse(List nums) { + int count = 0; // 通过索引遍历数组 for (var i = 0; i < nums.length; i++) { - count++; + count += nums[i]; } - // 直接遍历数组 - for (var num in nums) { - count++; + // 直接遍历数组元素 + for (int num in nums) { + count += num; } // 通过 forEach 方法遍历数组 - nums.forEach((element) { - count++; + nums.forEach((num) { + count += num; }); } /* 在数组中查找指定元素 */ -int find(List nums, int target) { +int find(List nums, int target) { for (var i = 0; i < nums.length; i++) { if (nums[i] == target) return i; } @@ -77,7 +77,7 @@ void main() { /* 初始化数组 */ var arr = List.filled(5, 0); print('数组 arr = $arr'); - List nums = [1, 3, 2, 5, 4]; + List nums = [1, 3, 2, 5, 4]; print('数组 nums = $nums'); /* 随机访问 */ @@ -96,7 +96,7 @@ void main() { remove(nums, 2); print("删除索引 2 处的元素,得到 nums = $nums"); - /* 遍历元素 */ + /* 遍历数组 */ traverse(nums); /* 查找元素 */ diff --git a/codes/dart/chapter_array_and_linkedlist/list.dart b/codes/dart/chapter_array_and_linkedlist/list.dart index f0b2e83cf..784cf37f6 100644 --- a/codes/dart/chapter_array_and_linkedlist/list.dart +++ b/codes/dart/chapter_array_and_linkedlist/list.dart @@ -43,12 +43,12 @@ void main() { /* 通过索引遍历列表 */ int count = 0; for (var i = 0; i < nums.length; i++) { - count++; + count += nums[i]; } /* 直接遍历列表元素 */ count = 0; - for (var n in nums) { - count++; + for (var x in nums) { + count += x; } /* 拼接两个列表 */ diff --git a/codes/dart/chapter_dynamic_programming/climbing_stairs_backtrack.dart b/codes/dart/chapter_dynamic_programming/climbing_stairs_backtrack.dart index 5bae93d38..3300fbd49 100644 --- a/codes/dart/chapter_dynamic_programming/climbing_stairs_backtrack.dart +++ b/codes/dart/chapter_dynamic_programming/climbing_stairs_backtrack.dart @@ -13,7 +13,7 @@ void backtrack(List choices, int state, int n, List res) { // 遍历所有选择 for (int choice in choices) { // 剪枝:不允许越过第 n 阶 - if (state + choice > n) break; + if (state + choice > n) continue; // 尝试:做出选择,更新状态 backtrack(choices, state + choice, n, res); // 回退 diff --git a/codes/docker-compose.yml b/codes/docker-compose.yml index e003ab034..7c11b7c5b 100644 --- a/codes/docker-compose.yml +++ b/codes/docker-compose.yml @@ -6,7 +6,7 @@ services: args: # 设置需要安装的语言,使用空格隔开 # Set the languages to be installed, separated by spaces - LANGS: "python cpp csharp" + LANGS: "python cpp java csharp" image: hello-algo-code container_name: hello-algo-code stdin_open: true diff --git a/codes/go/chapter_array_and_linkedlist/array.go b/codes/go/chapter_array_and_linkedlist/array.go index 114281854..792dab618 100644 --- a/codes/go/chapter_array_and_linkedlist/array.go +++ b/codes/go/chapter_array_and_linkedlist/array.go @@ -52,12 +52,17 @@ func traverse(nums []int) { 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 } } diff --git a/codes/go/chapter_array_and_linkedlist/list_test.go b/codes/go/chapter_array_and_linkedlist/list_test.go index ce83a2db4..b07a41eea 100644 --- a/codes/go/chapter_array_and_linkedlist/list_test.go +++ b/codes/go/chapter_array_and_linkedlist/list_test.go @@ -47,13 +47,12 @@ func TestList(t *testing.T) { /* 通过索引遍历列表 */ count := 0 for i := 0; i < len(nums); i++ { - count++ + count += nums[i] } - /* 直接遍历列表元素 */ count = 0 - for range nums { - count++ + for _, x := range nums { + count += x } /* 拼接两个列表 */ diff --git a/codes/go/chapter_dynamic_programming/climbing_stairs_backtrack.go b/codes/go/chapter_dynamic_programming/climbing_stairs_backtrack.go index 8049775d5..b77a07b77 100644 --- a/codes/go/chapter_dynamic_programming/climbing_stairs_backtrack.go +++ b/codes/go/chapter_dynamic_programming/climbing_stairs_backtrack.go @@ -14,7 +14,7 @@ func backtrack(choices []int, state, n int, res []int) { for _, choice := range choices { // 剪枝:不允许越过第 n 阶 if state+choice > n { - break + continue } // 尝试:做出选择,更新状态 backtrack(choices, state+choice, n, res) diff --git a/codes/java/chapter_array_and_linkedlist/array.java b/codes/java/chapter_array_and_linkedlist/array.java index 0e330e46a..c0f73d039 100644 --- a/codes/java/chapter_array_and_linkedlist/array.java +++ b/codes/java/chapter_array_and_linkedlist/array.java @@ -54,11 +54,11 @@ public class array { int count = 0; // 通过索引遍历数组 for (int i = 0; i < nums.length; i++) { - count++; + count += nums[i]; } - // 直接遍历数组 + // 直接遍历数组元素 for (int num : nums) { - count++; + count += num; } } diff --git a/codes/java/chapter_array_and_linkedlist/list.java b/codes/java/chapter_array_and_linkedlist/list.java index 0a22ed06d..1c8dd7d7f 100644 --- a/codes/java/chapter_array_and_linkedlist/list.java +++ b/codes/java/chapter_array_and_linkedlist/list.java @@ -17,8 +17,8 @@ public class list { System.out.println("列表 nums = " + nums); /* 访问元素 */ - int x = nums.get(1); - System.out.println("访问索引 1 处的元素,得到 x = " + x); + int num = nums.get(1); + System.out.println("访问索引 1 处的元素,得到 num = " + num); /* 更新元素 */ nums.set(1, 0); @@ -47,13 +47,11 @@ public class list { /* 通过索引遍历列表 */ int count = 0; for (int i = 0; i < nums.size(); i++) { - count++; + count += nums.get(i); } - /* 直接遍历列表元素 */ - count = 0; - for (int num : nums) { - count++; + for (int x : nums) { + count += x; } /* 拼接两个列表 */ diff --git a/codes/java/chapter_dynamic_programming/climbing_stairs_backtrack.java b/codes/java/chapter_dynamic_programming/climbing_stairs_backtrack.java index fa302e25e..6e30b2726 100644 --- a/codes/java/chapter_dynamic_programming/climbing_stairs_backtrack.java +++ b/codes/java/chapter_dynamic_programming/climbing_stairs_backtrack.java @@ -18,7 +18,7 @@ public class climbing_stairs_backtrack { for (Integer choice : choices) { // 剪枝:不允许越过第 n 阶 if (state + choice > n) - break; + continue; // 尝试:做出选择,更新状态 backtrack(choices, state + choice, n, res); // 回退 diff --git a/codes/javascript/chapter_array_and_linkedlist/array.js b/codes/javascript/chapter_array_and_linkedlist/array.js index a6ce0b53e..4fdbab4c8 100644 --- a/codes/javascript/chapter_array_and_linkedlist/array.js +++ b/codes/javascript/chapter_array_and_linkedlist/array.js @@ -50,11 +50,11 @@ function traverse(nums) { let count = 0; // 通过索引遍历数组 for (let i = 0; i < nums.length; i++) { - count++; + count += nums[i]; } - // 直接遍历数组 + // 直接遍历数组元素 for (const num of nums) { - count += 1; + count += num; } } diff --git a/codes/javascript/chapter_array_and_linkedlist/list.js b/codes/javascript/chapter_array_and_linkedlist/list.js index e627a4277..281132eba 100644 --- a/codes/javascript/chapter_array_and_linkedlist/list.js +++ b/codes/javascript/chapter_array_and_linkedlist/list.js @@ -39,13 +39,12 @@ console.log(`删除索引 3 处的元素,得到 nums = ${nums}`); /* 通过索引遍历列表 */ let count = 0; for (let i = 0; i < nums.length; i++) { - count++; + count += nums[i]; } - /* 直接遍历列表元素 */ count = 0; -for (const n of nums) { - count++; +for (const x of nums) { + count += x; } /* 拼接两个列表 */ diff --git a/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js b/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js index 90eef731e..21e6699d8 100644 --- a/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js +++ b/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js @@ -11,7 +11,7 @@ function backtrack(choices, state, n, res) { // 遍历所有选择 for (const choice of choices) { // 剪枝:不允许越过第 n 阶 - if (state + choice > n) break; + if (state + choice > n) continue; // 尝试:做出选择,更新状态 backtrack(choices, state + choice, n, res); // 回退 diff --git a/codes/python/chapter_array_and_linkedlist/array.py b/codes/python/chapter_array_and_linkedlist/array.py index 0b917e76d..a6b9c76c0 100644 --- a/codes/python/chapter_array_and_linkedlist/array.py +++ b/codes/python/chapter_array_and_linkedlist/array.py @@ -50,13 +50,14 @@ def traverse(nums: list[int]): 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 def find(nums: list[int], target: int) -> int: diff --git a/codes/python/chapter_array_and_linkedlist/list.py b/codes/python/chapter_array_and_linkedlist/list.py index b9ad6d352..b2da5c194 100644 --- a/codes/python/chapter_array_and_linkedlist/list.py +++ b/codes/python/chapter_array_and_linkedlist/list.py @@ -38,16 +38,13 @@ if __name__ == "__main__": nums.pop(3) print("\n删除索引 3 处的元素,得到 nums =", nums) - # 遍历列表 - tmp = [] + # 通过索引遍历列表 + count = 0 for i in range(len(nums)): - tmp.append(nums[i]) - print(f"\n通过索引遍历列表得到 tmp = {tmp}") - - tmp.clear() + count += nums[i] + # 直接遍历列表元素 for num in nums: - tmp.append(num) - print(f"\n直接遍历列表元素得到 tmp = {tmp}") + count += num # 拼接两个列表 nums1 = [6, 8, 7, 10, 9] diff --git a/codes/python/chapter_dynamic_programming/climbing_stairs_backtrack.py b/codes/python/chapter_dynamic_programming/climbing_stairs_backtrack.py index 962df8514..5b43e741a 100644 --- a/codes/python/chapter_dynamic_programming/climbing_stairs_backtrack.py +++ b/codes/python/chapter_dynamic_programming/climbing_stairs_backtrack.py @@ -14,7 +14,7 @@ def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int: for choice in choices: # 剪枝:不允许越过第 n 阶 if state + choice > n: - break + continue # 尝试:做出选择,更新状态 backtrack(choices, state + choice, n, res) # 回退 diff --git a/codes/python/modules/print_util.py b/codes/python/modules/print_util.py index b210db3c6..4780009bc 100644 --- a/codes/python/modules/print_util.py +++ b/codes/python/modules/print_util.py @@ -35,7 +35,9 @@ def show_trunks(p: Trunk | None): print(p.str, end="") -def print_tree(root: TreeNode | None, prev: Trunk | None = None, is_right: bool = False): +def print_tree( + root: TreeNode | None, prev: Trunk | None = None, is_right: bool = False +): """ Print a binary tree This tree printer is borrowed from TECHIE DELIGHT diff --git a/codes/rust/chapter_array_and_linkedlist/array.rs b/codes/rust/chapter_array_and_linkedlist/array.rs index 3670ec58a..76f0f3f52 100644 --- a/codes/rust/chapter_array_and_linkedlist/array.rs +++ b/codes/rust/chapter_array_and_linkedlist/array.rs @@ -51,12 +51,12 @@ fn remove(nums: &mut Vec, index: usize) { 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; } } diff --git a/codes/rust/chapter_array_and_linkedlist/list.rs b/codes/rust/chapter_array_and_linkedlist/list.rs index 241c71094..e1f384777 100644 --- a/codes/rust/chapter_array_and_linkedlist/list.rs +++ b/codes/rust/chapter_array_and_linkedlist/list.rs @@ -8,26 +8,26 @@ include!("../include/include.rs"); /* Driver Code */ fn main() { - // 初始化列表 + // 初始化列表 let mut nums: Vec = vec![ 1, 3, 2, 5, 4 ]; print!("列表 nums = "); print_util::print_array(&nums); - // 访问元素 + // 访问元素 let num = nums[1]; println!("\n访问索引 1 处的元素,得到 num = {num}"); - // 更新元素 + // 更新元素 nums[1] = 0; print!("将索引 1 处的元素更新为 0 ,得到 nums = "); print_util::print_array(&nums); - // 清空列表 + // 清空列表 nums.clear(); print!("\n清空列表后 nums = "); print_util::print_array(&nums); - // 尾部添加元素 + // 尾部添加元素 nums.push(1); nums.push(3); nums.push(2); @@ -36,39 +36,35 @@ fn main() { print!("\n添加元素后 nums = "); print_util::print_array(&nums); - // 中间插入元素 + // 中间插入元素 nums.insert(3, 6); print!("\n在索引 3 处插入数字 6 ,得到 nums = "); print_util::print_array(&nums); - // 删除元素 + // 删除元素 nums.remove(3); print!("\n删除索引 3 处的元素,得到 nums = "); print_util::print_array(&nums); - // 通过索引遍历列表 + // 通过索引遍历列表 let mut _count = 0; - for _ in 0..nums.len() { - _count += 1; + for i in 0..nums.len() { + _count += nums[i]; } - // 直接遍历列表元素 _count = 0; - for _n in &nums { - _count += 1; - } - // 或者 - // nums.iter().for_each(|_| _count += 1); - // let _count = nums.iter().fold(0, |_count, _| _count + 1); + for x in &nums { + _count += x; + } - // 拼接两个列表 + // 拼接两个列表 let mut nums1 = vec![ 6, 8, 7, 10, 9 ]; nums.append(&mut nums1); // append(移动) 之后 nums1 为空! // nums.extend(&nums1); // extend(借用) nums1 能继续使用 print!("\n将列表 nums1 拼接到 nums 之后,得到 nums = "); print_util::print_array(&nums); - // 排序列表 + // 排序列表 nums.sort(); print!("\n排序列表后 nums = "); print_util::print_array(&nums); diff --git a/codes/rust/chapter_dynamic_programming/climbing_stairs_backtrack.rs b/codes/rust/chapter_dynamic_programming/climbing_stairs_backtrack.rs index 1e9443491..726d516c2 100644 --- a/codes/rust/chapter_dynamic_programming/climbing_stairs_backtrack.rs +++ b/codes/rust/chapter_dynamic_programming/climbing_stairs_backtrack.rs @@ -11,7 +11,7 @@ fn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) { // 遍历所有选择 for &choice in choices { // 剪枝:不允许越过第 n 阶 - if state + choice > n { break; } + if state + choice > n { continue; } // 尝试:做出选择,更新状态 backtrack(choices, state + choice, n, res); // 回退 diff --git a/codes/swift/chapter_array_and_linkedlist/array.swift b/codes/swift/chapter_array_and_linkedlist/array.swift index 61f95e959..0bbdeab5a 100644 --- a/codes/swift/chapter_array_and_linkedlist/array.swift +++ b/codes/swift/chapter_array_and_linkedlist/array.swift @@ -48,12 +48,12 @@ func remove(nums: inout [Int], index: Int) { 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 } } diff --git a/codes/swift/chapter_array_and_linkedlist/list.swift b/codes/swift/chapter_array_and_linkedlist/list.swift index 269cbce12..4d64f5578 100644 --- a/codes/swift/chapter_array_and_linkedlist/list.swift +++ b/codes/swift/chapter_array_and_linkedlist/list.swift @@ -42,14 +42,13 @@ enum List { /* 通过索引遍历列表 */ 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 x in nums { + count += x } /* 拼接两个列表 */ diff --git a/codes/swift/chapter_dynamic_programming/climbing_stairs_backtrack.swift b/codes/swift/chapter_dynamic_programming/climbing_stairs_backtrack.swift index 69469972e..2fcd71ee7 100644 --- a/codes/swift/chapter_dynamic_programming/climbing_stairs_backtrack.swift +++ b/codes/swift/chapter_dynamic_programming/climbing_stairs_backtrack.swift @@ -14,7 +14,7 @@ func backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) { for choice in choices { // 剪枝:不允许越过第 n 阶 if state + choice > n { - break + continue } backtrack(choices: choices, state: state + choice, n: n, res: &res) } diff --git a/codes/typescript/chapter_array_and_linkedlist/array.ts b/codes/typescript/chapter_array_and_linkedlist/array.ts index eb5475e36..79f854fdd 100644 --- a/codes/typescript/chapter_array_and_linkedlist/array.ts +++ b/codes/typescript/chapter_array_and_linkedlist/array.ts @@ -50,11 +50,11 @@ function traverse(nums: number[]): void { let count = 0; // 通过索引遍历数组 for (let i = 0; i < nums.length; i++) { - count++; + count += nums[i]; } - // 直接遍历数组 + // 直接遍历数组元素 for (const num of nums) { - count += 1; + count += num; } } diff --git a/codes/typescript/chapter_array_and_linkedlist/list.ts b/codes/typescript/chapter_array_and_linkedlist/list.ts index 93fbbb32b..af6bdb328 100644 --- a/codes/typescript/chapter_array_and_linkedlist/list.ts +++ b/codes/typescript/chapter_array_and_linkedlist/list.ts @@ -39,13 +39,12 @@ console.log(`删除索引 3 处的元素,得到 nums = ${nums}`); /* 通过索引遍历列表 */ let count = 0; for (let i = 0; i < nums.length; i++) { - count++; + count += nums[i]; } - /* 直接遍历列表元素 */ count = 0; -for (const n of nums) { - count++; +for (const x of nums) { + count += x; } /* 拼接两个列表 */ diff --git a/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts b/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts index 2e49885c7..ee370f298 100644 --- a/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts +++ b/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts @@ -16,7 +16,7 @@ function backtrack( // 遍历所有选择 for (const choice of choices) { // 剪枝:不允许越过第 n 阶 - if (state + choice > n) break; + if (state + choice > n) continue; // 尝试:做出选择,更新状态 backtrack(choices, state + choice, n, res); // 回退 diff --git a/codes/zig/chapter_array_and_linkedlist/array.zig b/codes/zig/chapter_array_and_linkedlist/array.zig index c13e72d55..b2814ed58 100644 --- a/codes/zig/chapter_array_and_linkedlist/array.zig +++ b/codes/zig/chapter_array_and_linkedlist/array.zig @@ -51,12 +51,12 @@ pub fn traverse(nums: []i32) void { // 通过索引遍历数组 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; } } diff --git a/codes/zig/chapter_array_and_linkedlist/list.zig b/codes/zig/chapter_array_and_linkedlist/list.zig index 9c81da4c4..db8d7e664 100644 --- a/codes/zig/chapter_array_and_linkedlist/list.zig +++ b/codes/zig/chapter_array_and_linkedlist/list.zig @@ -52,13 +52,12 @@ pub fn main() !void { 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) |x| { + count += x; } // 拼接两个列表 diff --git a/codes/zig/chapter_dynamic_programming/climbing_stairs_backtrack.zig b/codes/zig/chapter_dynamic_programming/climbing_stairs_backtrack.zig index b5adbbfbb..356ae2750 100644 --- a/codes/zig/chapter_dynamic_programming/climbing_stairs_backtrack.zig +++ b/codes/zig/chapter_dynamic_programming/climbing_stairs_backtrack.zig @@ -14,7 +14,7 @@ fn backtrack(choices: []i32, state: i32, n: i32, res: std.ArrayList(i32)) void { for (choices) |choice| { // 剪枝:不允许越过第 n 阶 if (state + choice > n) { - break; + continue; } // 尝试:做出选择,更新状态 backtrack(choices, state + choice, n, res); diff --git a/docs/chapter_appendix/index.md b/docs/chapter_appendix/index.md index 8f71d7503..55f077ee3 100644 --- a/docs/chapter_appendix/index.md +++ b/docs/chapter_appendix/index.md @@ -2,6 +2,6 @@

-![附录](../assets/covers/chapter_appendix.jpg){ width="600" } +![附录](../assets/covers/chapter_appendix.jpg)
diff --git a/docs/chapter_appendix/terminology.md b/docs/chapter_appendix/terminology.md index 80e6efa95..1da75fccc 100644 --- a/docs/chapter_appendix/terminology.md +++ b/docs/chapter_appendix/terminology.md @@ -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 | diff --git a/docs/chapter_array_and_linkedlist/array.assets/array_definition.png b/docs/chapter_array_and_linkedlist/array.assets/array_definition.png index 57700f27d..ca996b71f 100644 Binary files a/docs/chapter_array_and_linkedlist/array.assets/array_definition.png and b/docs/chapter_array_and_linkedlist/array.assets/array_definition.png differ diff --git a/docs/chapter_array_and_linkedlist/index.md b/docs/chapter_array_and_linkedlist/index.md index 872380c12..c6fe52800 100644 --- a/docs/chapter_array_and_linkedlist/index.md +++ b/docs/chapter_array_and_linkedlist/index.md @@ -1,10 +1,6 @@ # 数组与链表 -
- -![数组与链表](../assets/covers/chapter_array_and_linkedlist.jpg){ width="600" } - -
+![数组与链表](../assets/covers/chapter_array_and_linkedlist.jpg) !!! abstract diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index cdf7cdaa5..77c48d5af 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -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; } ``` diff --git a/docs/chapter_array_and_linkedlist/summary.md b/docs/chapter_array_and_linkedlist/summary.md index 8927a6f6a..38033cc3c 100644 --- a/docs/chapter_array_and_linkedlist/summary.md +++ b/docs/chapter_array_and_linkedlist/summary.md @@ -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)$ 呢?" diff --git a/docs/chapter_backtracking/index.md b/docs/chapter_backtracking/index.md index 9d5845eee..cfaaba97c 100644 --- a/docs/chapter_backtracking/index.md +++ b/docs/chapter_backtracking/index.md @@ -2,7 +2,7 @@
-![回溯](../assets/covers/chapter_backtracking.jpg){ width="600" } +![回溯](../assets/covers/chapter_backtracking.jpg)
diff --git a/docs/chapter_computational_complexity/index.md b/docs/chapter_computational_complexity/index.md index 5867040c5..7a3294d68 100644 --- a/docs/chapter_computational_complexity/index.md +++ b/docs/chapter_computational_complexity/index.md @@ -2,7 +2,7 @@
-![复杂度分析](../assets/covers/chapter_complexity_analysis.jpg){ width="600" } +![复杂度分析](../assets/covers/chapter_complexity_analysis.jpg)
diff --git a/docs/chapter_data_structure/index.md b/docs/chapter_data_structure/index.md index 2cfc9bcfe..14ef0c17f 100644 --- a/docs/chapter_data_structure/index.md +++ b/docs/chapter_data_structure/index.md @@ -2,7 +2,7 @@
-![数据结构](../assets/covers/chapter_data_structure.jpg){ width="600" } +![数据结构](../assets/covers/chapter_data_structure.jpg)
diff --git a/docs/chapter_data_structure/number_encoding.md b/docs/chapter_data_structure/number_encoding.md index d04c2b56d..94903af74 100644 --- a/docs/chapter_data_structure/number_encoding.md +++ b/docs/chapter_data_structure/number_encoding.md @@ -18,7 +18,7 @@ ![原码、反码与补码之间的相互转换](number_encoding.assets/1s_2s_complement.png) -「原码 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} diff --git a/docs/chapter_divide_and_conquer/index.md b/docs/chapter_divide_and_conquer/index.md index 4f7f0ca6d..1d828f090 100644 --- a/docs/chapter_divide_and_conquer/index.md +++ b/docs/chapter_divide_and_conquer/index.md @@ -2,7 +2,7 @@
-![分治](../assets/covers/chapter_divide_and_conquer.jpg){ width="600" } +![分治](../assets/covers/chapter_divide_and_conquer.jpg)
diff --git a/docs/chapter_dynamic_programming/index.md b/docs/chapter_dynamic_programming/index.md index c8a09a7af..b27ede90c 100644 --- a/docs/chapter_dynamic_programming/index.md +++ b/docs/chapter_dynamic_programming/index.md @@ -2,7 +2,7 @@
-![动态规划](../assets/covers/chapter_dynamic_programming.jpg){ width="600" } +![动态规划](../assets/covers/chapter_dynamic_programming.jpg)
diff --git a/docs/chapter_graph/index.md b/docs/chapter_graph/index.md index 511e6e2ad..d3e7cb9fa 100644 --- a/docs/chapter_graph/index.md +++ b/docs/chapter_graph/index.md @@ -2,7 +2,7 @@
-![图](../assets/covers/chapter_graph.jpg){ width="600" } +![图](../assets/covers/chapter_graph.jpg)
diff --git a/docs/chapter_greedy/index.md b/docs/chapter_greedy/index.md index b2f4f3d85..ea86650e8 100644 --- a/docs/chapter_greedy/index.md +++ b/docs/chapter_greedy/index.md @@ -2,7 +2,7 @@
-![贪心](../assets/covers/chapter_greedy.jpg){ width="600" } +![贪心](../assets/covers/chapter_greedy.jpg)
diff --git a/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_short_board.png b/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_short_board.png index 7c08655ca..01b44bea7 100644 Binary files a/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_short_board.png and b/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_short_board.png differ diff --git a/docs/chapter_hashing/index.md b/docs/chapter_hashing/index.md index dd2235ebd..b7568bbad 100644 --- a/docs/chapter_hashing/index.md +++ b/docs/chapter_hashing/index.md @@ -2,7 +2,7 @@
-![哈希表](../assets/covers/chapter_hashing.jpg){ width="600" } +![哈希表](../assets/covers/chapter_hashing.jpg)
diff --git a/docs/chapter_heap/index.md b/docs/chapter_heap/index.md index 12fccd3fb..c5065ee1b 100644 --- a/docs/chapter_heap/index.md +++ b/docs/chapter_heap/index.md @@ -2,7 +2,7 @@
-![堆](../assets/covers/chapter_heap.jpg){ width="600" } +![堆](../assets/covers/chapter_heap.jpg)
diff --git a/docs/chapter_introduction/index.md b/docs/chapter_introduction/index.md index 510fcd05f..d2d770e7a 100644 --- a/docs/chapter_introduction/index.md +++ b/docs/chapter_introduction/index.md @@ -2,7 +2,7 @@
-![初识算法](../assets/covers/chapter_introduction.jpg){ width="600" } +![初识算法](../assets/covers/chapter_introduction.jpg)
diff --git a/docs/chapter_preface/index.md b/docs/chapter_preface/index.md index 4307e17fd..aac7d4444 100644 --- a/docs/chapter_preface/index.md +++ b/docs/chapter_preface/index.md @@ -2,7 +2,7 @@
-![前言](../assets/covers/chapter_preface.jpg){ width="600" } +![前言](../assets/covers/chapter_preface.jpg)
diff --git a/docs/chapter_searching/index.md b/docs/chapter_searching/index.md index 4939f1c55..8619d00c5 100644 --- a/docs/chapter_searching/index.md +++ b/docs/chapter_searching/index.md @@ -2,7 +2,7 @@
-![搜索](../assets/covers/chapter_searching.jpg){ width="600" } +![搜索](../assets/covers/chapter_searching.jpg)
diff --git a/docs/chapter_sorting/index.md b/docs/chapter_sorting/index.md index fb753b1e3..11d083256 100644 --- a/docs/chapter_sorting/index.md +++ b/docs/chapter_sorting/index.md @@ -2,7 +2,7 @@
-![排序](../assets/covers/chapter_sorting.jpg){ width="600" } +![排序](../assets/covers/chapter_sorting.jpg)
diff --git a/docs/chapter_stack_and_queue/index.md b/docs/chapter_stack_and_queue/index.md index e49dbba54..f3a7a936c 100644 --- a/docs/chapter_stack_and_queue/index.md +++ b/docs/chapter_stack_and_queue/index.md @@ -2,7 +2,7 @@
-![栈与队列](../assets/covers/chapter_stack_and_queue.jpg){ width="600" } +![栈与队列](../assets/covers/chapter_stack_and_queue.jpg)
diff --git a/docs/chapter_tree/index.md b/docs/chapter_tree/index.md index d1cdebded..74ffce97b 100644 --- a/docs/chapter_tree/index.md +++ b/docs/chapter_tree/index.md @@ -2,7 +2,7 @@
-![树](../assets/covers/chapter_tree.jpg){ width="600" } +![树](../assets/covers/chapter_tree.jpg)
diff --git a/docs/index.md b/docs/index.md index c78f830a9..c5d77e9bc 100644 --- a/docs/index.md +++ b/docs/index.md @@ -8,8 +8,8 @@ hide:

- - + +

《 Hello 算法 》

diff --git a/overrides/stylesheets/extra.css b/overrides/stylesheets/extra.css index 0db2dc4ff..e7a289b66 100644 --- a/overrides/stylesheets/extra.css +++ b/overrides/stylesheets/extra.css @@ -25,7 +25,7 @@ --md-default-fg-color: #adbac7; --md-default-bg-color: #22272e; - --md-code-bg-color: #1D2126; + --md-code-bg-color: #1d2126; --md-code-fg-color: #adbac7; --md-accent-fg-color: #aaa; @@ -43,6 +43,23 @@ color: var(--md-default-fg-color) !important; } +/* Figure class */ +.animation-figure { + border-radius: 0.63rem; + display: block; + margin: 0 auto; +} + +/* Cover image class */ +.cover-image { + width: 28rem; + height: auto; + border-radius: 0.3rem; + box-shadow: var(--md-shadow-z2); + display: block; + margin: 0 auto; +} + /* Center Markdown Tables (requires md_in_html extension) */ .center-table { text-align: center; @@ -82,12 +99,6 @@ text-transform: none; } -/* Image align center */ -.center { - display: block; - margin: 0 auto; -} - /* font-family setting for Win10 */ body { --md-text-font-family: -apple-system, BlinkMacSystemFont,