Polish some cotents.
This commit is contained in:
parent
335bc29af2
commit
399e5df39a
@ -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
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
int i = 0, j = len;
|
int i = 0, j = len;
|
||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
@ -52,7 +52,7 @@ int main() {
|
|||||||
printf("目标元素 6 的索引 = %d\n", index);
|
printf("目标元素 6 的索引 = %d\n", index);
|
||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
index = binarySearch1(nums, 10, target);
|
index = binarySearchLCRO(nums, 10, target);
|
||||||
printf("目标元素 6 的索引 = %d\n", index);
|
printf("目标元素 6 的索引 = %d\n", index);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -25,7 +25,7 @@ int binarySearch(vector<int> &nums, int target) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
int binarySearch1(vector<int> &nums, int target) {
|
int binarySearchLCRO(vector<int> &nums, int target) {
|
||||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
int i = 0, j = nums.size();
|
int i = 0, j = nums.size();
|
||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
@ -52,7 +52,7 @@ int main() {
|
|||||||
cout << "目标元素 6 的索引 = " << index << endl;
|
cout << "目标元素 6 的索引 = " << index << endl;
|
||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
index = binarySearch1(nums, target);
|
index = binarySearchLCRO(nums, target);
|
||||||
cout << "目标元素 6 的索引 = " << index << endl;
|
cout << "目标元素 6 的索引 = " << index << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -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
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
int i = 0, j = nums.Length;
|
int i = 0, j = nums.Length;
|
||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
@ -53,7 +53,7 @@ public class binary_search {
|
|||||||
Console.WriteLine("目标元素 6 的索引 = " + index);
|
Console.WriteLine("目标元素 6 的索引 = " + index);
|
||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
index = binarySearch1(nums, target);
|
index = binarySearchLCRO(nums, target);
|
||||||
Console.WriteLine("目标元素 6 的索引 = " + index);
|
Console.WriteLine("目标元素 6 的索引 = " + index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
i, j := 0, len(nums)
|
i, j := 0, len(nums)
|
||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
|
@ -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
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
int i = 0, j = nums.length;
|
int i = 0, j = nums.length;
|
||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
@ -52,7 +52,7 @@ public class binary_search {
|
|||||||
System.out.println("目标元素 6 的索引 = " + index);
|
System.out.println("目标元素 6 的索引 = " + index);
|
||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
index = binarySearch1(nums, target);
|
index = binarySearchLCRO(nums, target);
|
||||||
System.out.println("目标元素 6 的索引 = " + index);
|
System.out.println("目标元素 6 的索引 = " + index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ function binarySearch(nums, target) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
function binarySearch1(nums, target) {
|
function binarySearchLCRO(nums, target) {
|
||||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
let i = 0,
|
let i = 0,
|
||||||
j = nums.length;
|
j = nums.length;
|
||||||
@ -56,5 +56,5 @@ let index = binarySearch(nums, target);
|
|||||||
console.log('目标元素 6 的索引 = ' + index);
|
console.log('目标元素 6 的索引 = ' + index);
|
||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
index = binarySearch1(nums, target);
|
index = binarySearchLCRO(nums, target);
|
||||||
console.log('目标元素 6 的索引 = ' + index);
|
console.log('目标元素 6 的索引 = ' + index);
|
||||||
|
@ -20,7 +20,7 @@ def binary_search(nums: list[int], target: int) -> int:
|
|||||||
return -1 # 未找到目标元素,返回 -1
|
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
|
# 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
i, j = 0, len(nums)
|
i, j = 0, len(nums)
|
||||||
@ -46,5 +46,5 @@ if __name__ == "__main__":
|
|||||||
print("目标元素 6 的索引 = ", index)
|
print("目标元素 6 的索引 = ", index)
|
||||||
|
|
||||||
# 二分查找(左闭右开)
|
# 二分查找(左闭右开)
|
||||||
index: int = binary_search1(nums, target)
|
index: int = binary_search_lcro(nums, target)
|
||||||
print("目标元素 6 的索引 = ", index)
|
print("目标元素 6 的索引 = ", index)
|
||||||
|
@ -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
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let mut j = nums.len();
|
let mut j = nums.len();
|
||||||
@ -54,6 +54,6 @@ pub fn main() {
|
|||||||
println!("目标元素 6 的索引 = {index}");
|
println!("目标元素 6 的索引 = {index}");
|
||||||
|
|
||||||
// 二分查找(左闭右开)
|
// 二分查找(左闭右开)
|
||||||
index = binary_search1(&nums, target);
|
index = binary_search_lcro(&nums, target);
|
||||||
println!("目标元素 6 的索引 = {index}");
|
println!("目标元素 6 的索引 = {index}");
|
||||||
}
|
}
|
@ -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
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
var i = 0
|
var i = 0
|
||||||
var j = nums.count
|
var j = nums.count
|
||||||
@ -56,7 +56,7 @@ enum BinarySearch {
|
|||||||
print("目标元素 6 的索引 = \(index)")
|
print("目标元素 6 的索引 = \(index)")
|
||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
index = binarySearch1(nums: nums, target: target)
|
index = binarySearchLCRO(nums: nums, target: target)
|
||||||
print("目标元素 6 的索引 = \(index)")
|
print("目标元素 6 的索引 = \(index)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
let i = 0,
|
let i = 0,
|
||||||
j = nums.length;
|
j = nums.length;
|
||||||
@ -59,7 +59,7 @@ let index = binarySearch(nums, target);
|
|||||||
console.info('目标元素 6 的索引 = %d', index);
|
console.info('目标元素 6 的索引 = %d', index);
|
||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
index = binarySearch1(nums, target);
|
index = binarySearchLCRO(nums, target);
|
||||||
console.info('目标元素 6 的索引 = %d', index);
|
console.info('目标元素 6 的索引 = %d', index);
|
||||||
|
|
||||||
export {};
|
export {};
|
||||||
|
@ -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
|
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
var j: usize = nums.items.len;
|
var j: usize = nums.items.len;
|
||||||
@ -57,7 +57,7 @@ pub fn main() !void {
|
|||||||
std.debug.print("目标元素 6 的索引 = {}\n", .{index});
|
std.debug.print("目标元素 6 的索引 = {}\n", .{index});
|
||||||
|
|
||||||
// 二分查找(左闭右开)
|
// 二分查找(左闭右开)
|
||||||
index = binarySearch1(i32, nums, target);
|
index = binarySearchLCRO(i32, nums, target);
|
||||||
std.debug.print("目标元素 6 的索引 = {}\n", .{index});
|
std.debug.print("目标元素 6 的索引 = {}\n", .{index});
|
||||||
|
|
||||||
_ = try std.io.getStdIn().reader().readByte();
|
_ = try std.io.getStdIn().reader().readByte();
|
||||||
|
@ -194,61 +194,61 @@ $$
|
|||||||
=== "Java"
|
=== "Java"
|
||||||
|
|
||||||
```java title="binary_search.java"
|
```java title="binary_search.java"
|
||||||
[class]{binary_search}-[func]{binarySearch1}
|
[class]{binary_search}-[func]{binarySearchLCRO}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C++"
|
=== "C++"
|
||||||
|
|
||||||
```cpp title="binary_search.cpp"
|
```cpp title="binary_search.cpp"
|
||||||
[class]{}-[func]{binarySearch1}
|
[class]{}-[func]{binarySearchLCRO}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Python"
|
=== "Python"
|
||||||
|
|
||||||
```python title="binary_search.py"
|
```python title="binary_search.py"
|
||||||
[class]{}-[func]{binary_search1}
|
[class]{}-[func]{binary_search_lcro}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Go"
|
=== "Go"
|
||||||
|
|
||||||
```go title="binary_search.go"
|
```go title="binary_search.go"
|
||||||
[class]{}-[func]{binarySearch1}
|
[class]{}-[func]{binarySearchLCRO}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
|
||||||
```javascript title="binary_search.js"
|
```javascript title="binary_search.js"
|
||||||
[class]{}-[func]{binarySearch1}
|
[class]{}-[func]{binarySearchLCRO}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "TypeScript"
|
=== "TypeScript"
|
||||||
|
|
||||||
```typescript title="binary_search.ts"
|
```typescript title="binary_search.ts"
|
||||||
[class]{}-[func]{binarySearch1}
|
[class]{}-[func]{binarySearchLCRO}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C"
|
=== "C"
|
||||||
|
|
||||||
```c title="binary_search.c"
|
```c title="binary_search.c"
|
||||||
[class]{}-[func]{binarySearch1}
|
[class]{}-[func]{binarySearchLCRO}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "C#"
|
=== "C#"
|
||||||
|
|
||||||
```csharp title="binary_search.cs"
|
```csharp title="binary_search.cs"
|
||||||
[class]{binary_search}-[func]{binarySearch1}
|
[class]{binary_search}-[func]{binarySearchLCRO}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Swift"
|
=== "Swift"
|
||||||
|
|
||||||
```swift title="binary_search.swift"
|
```swift title="binary_search.swift"
|
||||||
[class]{}-[func]{binarySearch1}
|
[class]{}-[func]{binarySearchLCRO}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "Zig"
|
=== "Zig"
|
||||||
|
|
||||||
```zig title="binary_search.zig"
|
```zig title="binary_search.zig"
|
||||||
[class]{}-[func]{binarySearch1}
|
[class]{}-[func]{binarySearchLCRO}
|
||||||
```
|
```
|
||||||
|
|
||||||
对比这两种代码写法,我们可以发现以下不同点:
|
对比这两种代码写法,我们可以发现以下不同点:
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# 字符集与编码
|
# 字符编码 *
|
||||||
|
|
||||||
在计算机中,所有数据都是以二进制数的形式存储的,字符 `char` 也不例外。为了表示字符,我们需要建立一套「字符集」,规定每个字符和二进制数之间的一一对应关系。有了字符集之后,计算机就可以通过查表完成二进制数到字符的转换。
|
在计算机中,所有数据都是以二进制数的形式存储的,字符 `char` 也不例外。为了表示字符,我们需要建立一套「字符集」,规定每个字符和二进制数之间的一一对应关系。有了字符集之后,计算机就可以通过查表完成二进制数到字符的转换。
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# 小结
|
# 小结
|
||||||
|
|
||||||
## 快速回顾
|
## 知识回顾
|
||||||
|
|
||||||
### 数据结构分类
|
### 数据结构分类
|
||||||
|
|
||||||
|
@ -397,9 +397,11 @@
|
|||||||
以学生数据 `key 学号 -> value 姓名` 为例,我们可以设计如下哈希函数:
|
以学生数据 `key 学号 -> value 姓名` 为例,我们可以设计如下哈希函数:
|
||||||
|
|
||||||
$$
|
$$
|
||||||
f(x) = x \% 100
|
f(x) = x \bmod {100}
|
||||||
$$
|
$$
|
||||||
|
|
||||||
|
其中 $\bmod$ 表示取余运算。
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
=== "Java"
|
=== "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
|
f(12836) = f(20336) = 36
|
||||||
|
@ -17,10 +17,10 @@
|
|||||||
下面来剖析代码实现。对于一个 $d$ 进制的数字 $x$ ,要获取其第 $k$ 位 $x_k$ ,可以使用以下计算公式:
|
下面来剖析代码实现。对于一个 $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$ 位进行排序。
|
此外,我们需要小幅改动计数排序代码,使之可以根据数字的第 $k$ 位进行排序。
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user