From 399e5df39affd12a7e9cc20d982a2a3bdbfcb581 Mon Sep 17 00:00:00 2001 From: krahets Date: Thu, 18 May 2023 20:27:58 +0800 Subject: [PATCH] Polish some cotents. --- codes/c/chapter_binary_search/binary_search.c | 4 ++-- .../chapter_binary_search/binary_search.cpp | 4 ++-- .../chapter_binary_search/binary_search.cs | 4 ++-- .../go/chapter_binary_search/binary_search.go | 2 +- .../chapter_binary_search/binary_search.java | 4 ++-- .../chapter_binary_search/binary_search.js | 4 ++-- .../chapter_binary_search/binary_search.py | 4 ++-- .../chapter_binary_search/binary_search.rs | 4 ++-- .../chapter_binary_search/binary_search.swift | 4 ++-- .../chapter_binary_search/binary_search.ts | 4 ++-- .../chapter_binary_search/binary_search.zig | 4 ++-- docs/chapter_binary_search/binary_search.md | 20 +++++++++---------- .../character_encoding.md | 2 +- docs/chapter_data_structure/summary.md | 2 +- docs/chapter_hashing/hash_map.md | 6 ++++-- docs/chapter_sorting/radix_sort.md | 4 ++-- 16 files changed, 39 insertions(+), 37 deletions(-) diff --git a/codes/c/chapter_binary_search/binary_search.c b/codes/c/chapter_binary_search/binary_search.c index 250b04e60..f9183860a 100644 --- a/codes/c/chapter_binary_search/binary_search.c +++ b/codes/c/chapter_binary_search/binary_search.c @@ -25,7 +25,7 @@ int binarySearch(int *nums, int len, int target) { } /* 二分查找(左闭右开) */ -int binarySearch1(int *nums, int len, int target) { +int binarySearchLCRO(int *nums, int len, int target) { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 int i = 0, j = len; // 循环,当搜索区间为空时跳出(当 i = j 时为空) @@ -52,7 +52,7 @@ int main() { printf("目标元素 6 的索引 = %d\n", index); /* 二分查找(左闭右开) */ - index = binarySearch1(nums, 10, target); + index = binarySearchLCRO(nums, 10, target); printf("目标元素 6 的索引 = %d\n", index); return 0; diff --git a/codes/cpp/chapter_binary_search/binary_search.cpp b/codes/cpp/chapter_binary_search/binary_search.cpp index 8d7c41597..60a26305c 100644 --- a/codes/cpp/chapter_binary_search/binary_search.cpp +++ b/codes/cpp/chapter_binary_search/binary_search.cpp @@ -25,7 +25,7 @@ int binarySearch(vector &nums, int target) { } /* 二分查找(左闭右开) */ -int binarySearch1(vector &nums, int target) { +int binarySearchLCRO(vector &nums, int target) { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 int i = 0, j = nums.size(); // 循环,当搜索区间为空时跳出(当 i = j 时为空) @@ -52,7 +52,7 @@ int main() { cout << "目标元素 6 的索引 = " << index << endl; /* 二分查找(左闭右开) */ - index = binarySearch1(nums, target); + index = binarySearchLCRO(nums, target); cout << "目标元素 6 的索引 = " << index << endl; return 0; diff --git a/codes/csharp/chapter_binary_search/binary_search.cs b/codes/csharp/chapter_binary_search/binary_search.cs index 6712c5e74..358db04e1 100644 --- a/codes/csharp/chapter_binary_search/binary_search.cs +++ b/codes/csharp/chapter_binary_search/binary_search.cs @@ -26,7 +26,7 @@ public class binary_search { } /* 二分查找(左闭右开) */ - static int binarySearch1(int[] nums, int target) { + static int binarySearchLCRO(int[] nums, int target) { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 int i = 0, j = nums.Length; // 循环,当搜索区间为空时跳出(当 i = j 时为空) @@ -53,7 +53,7 @@ public class binary_search { Console.WriteLine("目标元素 6 的索引 = " + index); /* 二分查找(左闭右开) */ - index = binarySearch1(nums, target); + index = binarySearchLCRO(nums, target); Console.WriteLine("目标元素 6 的索引 = " + index); } } diff --git a/codes/go/chapter_binary_search/binary_search.go b/codes/go/chapter_binary_search/binary_search.go index 159524bd9..a1f61b27b 100644 --- a/codes/go/chapter_binary_search/binary_search.go +++ b/codes/go/chapter_binary_search/binary_search.go @@ -24,7 +24,7 @@ func binarySearch(nums []int, target int) int { } /* 二分查找(左闭右开) */ -func binarySearch1(nums []int, target int) int { +func binarySearchLCRO(nums []int, target int) int { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 i, j := 0, len(nums) // 循环,当搜索区间为空时跳出(当 i = j 时为空) diff --git a/codes/java/chapter_binary_search/binary_search.java b/codes/java/chapter_binary_search/binary_search.java index 3e20efb56..f9608a663 100644 --- a/codes/java/chapter_binary_search/binary_search.java +++ b/codes/java/chapter_binary_search/binary_search.java @@ -26,7 +26,7 @@ public class binary_search { } /* 二分查找(左闭右开) */ - static int binarySearch1(int[] nums, int target) { + static int binarySearchLCRO(int[] nums, int target) { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 int i = 0, j = nums.length; // 循环,当搜索区间为空时跳出(当 i = j 时为空) @@ -52,7 +52,7 @@ public class binary_search { System.out.println("目标元素 6 的索引 = " + index); /* 二分查找(左闭右开) */ - index = binarySearch1(nums, target); + index = binarySearchLCRO(nums, target); System.out.println("目标元素 6 的索引 = " + index); } } diff --git a/codes/javascript/chapter_binary_search/binary_search.js b/codes/javascript/chapter_binary_search/binary_search.js index d9452a5d9..2e33ac0b8 100644 --- a/codes/javascript/chapter_binary_search/binary_search.js +++ b/codes/javascript/chapter_binary_search/binary_search.js @@ -26,7 +26,7 @@ function binarySearch(nums, target) { } /* 二分查找(左闭右开) */ -function binarySearch1(nums, target) { +function binarySearchLCRO(nums, target) { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 let i = 0, j = nums.length; @@ -56,5 +56,5 @@ let index = binarySearch(nums, target); console.log('目标元素 6 的索引 = ' + index); /* 二分查找(左闭右开) */ -index = binarySearch1(nums, target); +index = binarySearchLCRO(nums, target); console.log('目标元素 6 的索引 = ' + index); diff --git a/codes/python/chapter_binary_search/binary_search.py b/codes/python/chapter_binary_search/binary_search.py index f95d39435..056e9baf4 100644 --- a/codes/python/chapter_binary_search/binary_search.py +++ b/codes/python/chapter_binary_search/binary_search.py @@ -20,7 +20,7 @@ def binary_search(nums: list[int], target: int) -> int: return -1 # 未找到目标元素,返回 -1 -def binary_search1(nums: list[int], target: int) -> int: +def binary_search_lcro(nums: list[int], target: int) -> int: """二分查找(左闭右开)""" # 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 i, j = 0, len(nums) @@ -46,5 +46,5 @@ if __name__ == "__main__": print("目标元素 6 的索引 = ", index) # 二分查找(左闭右开) - index: int = binary_search1(nums, target) + index: int = binary_search_lcro(nums, target) print("目标元素 6 的索引 = ", index) diff --git a/codes/rust/chapter_binary_search/binary_search.rs b/codes/rust/chapter_binary_search/binary_search.rs index ada931b75..6a74c9067 100644 --- a/codes/rust/chapter_binary_search/binary_search.rs +++ b/codes/rust/chapter_binary_search/binary_search.rs @@ -25,7 +25,7 @@ fn binary_search(nums: &[i32], target: i32) -> i32 { } /* 二分查找(左闭右开) */ -fn binary_search1(nums: &[i32], target: i32) -> i32 { +fn binary_search_lcro(nums: &[i32], target: i32) -> i32 { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 let mut i = 0; let mut j = nums.len(); @@ -54,6 +54,6 @@ pub fn main() { println!("目标元素 6 的索引 = {index}"); // 二分查找(左闭右开) - index = binary_search1(&nums, target); + index = binary_search_lcro(&nums, target); println!("目标元素 6 的索引 = {index}"); } \ No newline at end of file diff --git a/codes/swift/chapter_binary_search/binary_search.swift b/codes/swift/chapter_binary_search/binary_search.swift index 25930b238..4eddcc6fa 100644 --- a/codes/swift/chapter_binary_search/binary_search.swift +++ b/codes/swift/chapter_binary_search/binary_search.swift @@ -25,7 +25,7 @@ func binarySearch(nums: [Int], target: Int) -> Int { } /* 二分查找(左闭右开) */ -func binarySearch1(nums: [Int], target: Int) -> Int { +func binarySearchLCRO(nums: [Int], target: Int) -> Int { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 var i = 0 var j = nums.count @@ -56,7 +56,7 @@ enum BinarySearch { print("目标元素 6 的索引 = \(index)") /* 二分查找(左闭右开) */ - index = binarySearch1(nums: nums, target: target) + index = binarySearchLCRO(nums: nums, target: target) print("目标元素 6 的索引 = \(index)") } } diff --git a/codes/typescript/chapter_binary_search/binary_search.ts b/codes/typescript/chapter_binary_search/binary_search.ts index dee753aad..bc305b3c4 100644 --- a/codes/typescript/chapter_binary_search/binary_search.ts +++ b/codes/typescript/chapter_binary_search/binary_search.ts @@ -28,7 +28,7 @@ function binarySearch(nums: number[], target: number): number { } /* 二分查找(左闭右开) */ -function binarySearch1(nums: number[], target: number): number { +function binarySearchLCRO(nums: number[], target: number): number { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 let i = 0, j = nums.length; @@ -59,7 +59,7 @@ let index = binarySearch(nums, target); console.info('目标元素 6 的索引 = %d', index); /* 二分查找(左闭右开) */ -index = binarySearch1(nums, target); +index = binarySearchLCRO(nums, target); console.info('目标元素 6 的索引 = %d', index); export {}; diff --git a/codes/zig/chapter_binary_search/binary_search.zig b/codes/zig/chapter_binary_search/binary_search.zig index 300b7c491..ac0bf0351 100644 --- a/codes/zig/chapter_binary_search/binary_search.zig +++ b/codes/zig/chapter_binary_search/binary_search.zig @@ -26,7 +26,7 @@ fn binarySearch(comptime T: type, nums: std.ArrayList(T), target: T) T { } // 二分查找(左闭右开) -fn binarySearch1(comptime T: type, nums: std.ArrayList(T), target: T) T { +fn binarySearchLCRO(comptime T: type, nums: std.ArrayList(T), target: T) T { // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 var i: usize = 0; var j: usize = nums.items.len; @@ -57,7 +57,7 @@ pub fn main() !void { std.debug.print("目标元素 6 的索引 = {}\n", .{index}); // 二分查找(左闭右开) - index = binarySearch1(i32, nums, target); + index = binarySearchLCRO(i32, nums, target); std.debug.print("目标元素 6 的索引 = {}\n", .{index}); _ = try std.io.getStdIn().reader().readByte(); diff --git a/docs/chapter_binary_search/binary_search.md b/docs/chapter_binary_search/binary_search.md index adbfa0ccb..3649facc2 100755 --- a/docs/chapter_binary_search/binary_search.md +++ b/docs/chapter_binary_search/binary_search.md @@ -194,61 +194,61 @@ $$ === "Java" ```java title="binary_search.java" - [class]{binary_search}-[func]{binarySearch1} + [class]{binary_search}-[func]{binarySearchLCRO} ``` === "C++" ```cpp title="binary_search.cpp" - [class]{}-[func]{binarySearch1} + [class]{}-[func]{binarySearchLCRO} ``` === "Python" ```python title="binary_search.py" - [class]{}-[func]{binary_search1} + [class]{}-[func]{binary_search_lcro} ``` === "Go" ```go title="binary_search.go" - [class]{}-[func]{binarySearch1} + [class]{}-[func]{binarySearchLCRO} ``` === "JavaScript" ```javascript title="binary_search.js" - [class]{}-[func]{binarySearch1} + [class]{}-[func]{binarySearchLCRO} ``` === "TypeScript" ```typescript title="binary_search.ts" - [class]{}-[func]{binarySearch1} + [class]{}-[func]{binarySearchLCRO} ``` === "C" ```c title="binary_search.c" - [class]{}-[func]{binarySearch1} + [class]{}-[func]{binarySearchLCRO} ``` === "C#" ```csharp title="binary_search.cs" - [class]{binary_search}-[func]{binarySearch1} + [class]{binary_search}-[func]{binarySearchLCRO} ``` === "Swift" ```swift title="binary_search.swift" - [class]{}-[func]{binarySearch1} + [class]{}-[func]{binarySearchLCRO} ``` === "Zig" ```zig title="binary_search.zig" - [class]{}-[func]{binarySearch1} + [class]{}-[func]{binarySearchLCRO} ``` 对比这两种代码写法,我们可以发现以下不同点: diff --git a/docs/chapter_data_structure/character_encoding.md b/docs/chapter_data_structure/character_encoding.md index a6051ad13..983dda6aa 100644 --- a/docs/chapter_data_structure/character_encoding.md +++ b/docs/chapter_data_structure/character_encoding.md @@ -1,4 +1,4 @@ -# 字符集与编码 +# 字符编码 * 在计算机中,所有数据都是以二进制数的形式存储的,字符 `char` 也不例外。为了表示字符,我们需要建立一套「字符集」,规定每个字符和二进制数之间的一一对应关系。有了字符集之后,计算机就可以通过查表完成二进制数到字符的转换。 diff --git a/docs/chapter_data_structure/summary.md b/docs/chapter_data_structure/summary.md index 99e2c9d72..9fc9062df 100644 --- a/docs/chapter_data_structure/summary.md +++ b/docs/chapter_data_structure/summary.md @@ -1,6 +1,6 @@ # 小结 -## 快速回顾 +## 知识回顾 ### 数据结构分类 diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index df063edd8..5a0716980 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -397,9 +397,11 @@ 以学生数据 `key 学号 -> value 姓名` 为例,我们可以设计如下哈希函数: $$ -f(x) = x \% 100 +f(x) = x \bmod {100} $$ +其中 $\bmod$ 表示取余运算。 + ![哈希函数工作原理](hash_map.assets/hash_function.png) === "Java" @@ -484,7 +486,7 @@ $$ ## 哈希冲突 -细心的你可能已经注意到,**在某些情况下,哈希函数 $f(x) = x % 100$ 可能无法正常工作**。具体来说,当输入的 key 后两位相同时,哈希函数的计算结果也会相同,从而指向同一个 value 。例如,查询学号为 $12836$ 和 $20336$ 的两个学生时,我们得到: +细心的你可能已经注意到,**在某些情况下,哈希函数 $f(x) = x \bmod 100$ 可能无法正常工作**。具体来说,当输入的 key 后两位相同时,哈希函数的计算结果也会相同,从而指向同一个 value 。例如,查询学号为 $12836$ 和 $20336$ 的两个学生时,我们得到: $$ f(12836) = f(20336) = 36 diff --git a/docs/chapter_sorting/radix_sort.md b/docs/chapter_sorting/radix_sort.md index 3af811421..444cde80e 100644 --- a/docs/chapter_sorting/radix_sort.md +++ b/docs/chapter_sorting/radix_sort.md @@ -17,10 +17,10 @@ 下面来剖析代码实现。对于一个 $d$ 进制的数字 $x$ ,要获取其第 $k$ 位 $x_k$ ,可以使用以下计算公式: $$ -x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \mod d +x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \bmod d $$ -其中 $\lfloor a \rfloor$ 表示对浮点数 $a$ 向下取整,而 $\mod d$ 表示对 $d$ 取余。对于学号数据,$d = 10$ 且 $k \in [1, 8]$ 。 +其中 $\lfloor a \rfloor$ 表示对浮点数 $a$ 向下取整,而 $\bmod \space d$ 表示对 $d$ 取余。对于学号数据,$d = 10$ 且 $k \in [1, 8]$ 。 此外,我们需要小幅改动计数排序代码,使之可以根据数字的第 $k$ 位进行排序。