mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Sort the coding languages by applications. (#721)
This commit is contained in:
@ -45,16 +45,16 @@ index = hash(key) % capacity
|
||||
- **异或哈希**:将输入数据的每个元素通过异或操作累积到一个哈希值中。
|
||||
- **旋转哈希**:将每个字符的 ASCII 码累积到一个哈希值中,每次累积之前都会对哈希值进行旋转操作。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="simple_hash.java"
|
||||
[class]{simple_hash}-[func]{addHash}
|
||||
```python title="simple_hash.py"
|
||||
[class]{}-[func]{add_hash}
|
||||
|
||||
[class]{simple_hash}-[func]{mulHash}
|
||||
[class]{}-[func]{mul_hash}
|
||||
|
||||
[class]{simple_hash}-[func]{xorHash}
|
||||
[class]{}-[func]{xor_hash}
|
||||
|
||||
[class]{simple_hash}-[func]{rotHash}
|
||||
[class]{}-[func]{rot_hash}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@ -69,16 +69,28 @@ index = hash(key) % capacity
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="simple_hash.py"
|
||||
[class]{}-[func]{add_hash}
|
||||
```java title="simple_hash.java"
|
||||
[class]{simple_hash}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mul_hash}
|
||||
[class]{simple_hash}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xor_hash}
|
||||
[class]{simple_hash}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rot_hash}
|
||||
[class]{simple_hash}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="simple_hash.cs"
|
||||
[class]{simple_hash}-[func]{addHash}
|
||||
|
||||
[class]{simple_hash}-[func]{mulHash}
|
||||
|
||||
[class]{simple_hash}-[func]{xorHash}
|
||||
|
||||
[class]{simple_hash}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -93,6 +105,18 @@ index = hash(key) % capacity
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="simple_hash.swift"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="simple_hash.js"
|
||||
@ -117,54 +141,6 @@ index = hash(key) % capacity
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="simple_hash.c"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="simple_hash.cs"
|
||||
[class]{simple_hash}-[func]{addHash}
|
||||
|
||||
[class]{simple_hash}-[func]{mulHash}
|
||||
|
||||
[class]{simple_hash}-[func]{xorHash}
|
||||
|
||||
[class]{simple_hash}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="simple_hash.swift"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="simple_hash.zig"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="simple_hash.dart"
|
||||
@ -189,6 +165,30 @@ index = hash(key) % capacity
|
||||
[class]{}-[func]{rot_hash}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="simple_hash.c"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="simple_hash.zig"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
观察发现,每种哈希算法的最后一步都是对大质数 $1000000007$ 取模,以确保哈希值在合适的范围内。值得思考的是,为什么要强调对质数取模,或者说对合数取模的弊端是什么?这是一个有趣的问题。
|
||||
|
||||
先抛出结论:**当我们使用大质数作为模数时,可以最大化地保证哈希值的均匀分布**。因为质数不会与其他数字存在公约数,可以减少因取模操作而产生的周期性模式,从而避免哈希冲突。
|
||||
@ -252,57 +252,6 @@ $$
|
||||
|
||||
请注意,不同编程语言的内置哈希值计算函数的定义和方法不同。
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="built_in_hash.java"
|
||||
int num = 3;
|
||||
int hashNum = Integer.hashCode(num);
|
||||
// 整数 3 的哈希值为 3
|
||||
|
||||
boolean bol = true;
|
||||
int hashBol = Boolean.hashCode(bol);
|
||||
// 布尔量 true 的哈希值为 1231
|
||||
|
||||
double dec = 3.14159;
|
||||
int hashDec = Double.hashCode(dec);
|
||||
// 小数 3.14159 的哈希值为 -1340954729
|
||||
|
||||
String str = "Hello 算法";
|
||||
int hashStr = str.hashCode();
|
||||
// 字符串 Hello 算法 的哈希值为 -727081396
|
||||
|
||||
Object[] arr = { 12836, "小哈" };
|
||||
int hashTup = Arrays.hashCode(arr);
|
||||
// 数组 [12836, 小哈] 的哈希值为 1151158
|
||||
|
||||
ListNode obj = new ListNode(0);
|
||||
int hashObj = obj.hashCode();
|
||||
// 节点对象 utils.ListNode@7dc5e7b4 的哈希值为 2110121908
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="built_in_hash.cpp"
|
||||
int num = 3;
|
||||
size_t hashNum = hash<int>()(num);
|
||||
// 整数 3 的哈希值为 3
|
||||
|
||||
bool bol = true;
|
||||
size_t hashBol = hash<bool>()(bol);
|
||||
// 布尔量 1 的哈希值为 1
|
||||
|
||||
double dec = 3.14159;
|
||||
size_t hashDec = hash<double>()(dec);
|
||||
// 小数 3.14159 的哈希值为 4614256650576692846
|
||||
|
||||
string str = "Hello 算法";
|
||||
size_t hashStr = hash<string>()(str);
|
||||
// 字符串 Hello 算法 的哈希值为 15466937326284535026
|
||||
|
||||
// 在 C++ 中,内置 std:hash() 仅提供基本数据类型的哈希值计算
|
||||
// 数组、对象的哈希值计算需要自行实现
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="built_in_hash.py"
|
||||
@ -331,28 +280,55 @@ $$
|
||||
# 节点对象 <ListNode object at 0x1058fd810> 的哈希值为 274267521
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
=== "C++"
|
||||
|
||||
```go title="built_in_hash.go"
|
||||
```cpp title="built_in_hash.cpp"
|
||||
int num = 3;
|
||||
size_t hashNum = hash<int>()(num);
|
||||
// 整数 3 的哈希值为 3
|
||||
|
||||
bool bol = true;
|
||||
size_t hashBol = hash<bool>()(bol);
|
||||
// 布尔量 1 的哈希值为 1
|
||||
|
||||
double dec = 3.14159;
|
||||
size_t hashDec = hash<double>()(dec);
|
||||
// 小数 3.14159 的哈希值为 4614256650576692846
|
||||
|
||||
string str = "Hello 算法";
|
||||
size_t hashStr = hash<string>()(str);
|
||||
// 字符串 Hello 算法 的哈希值为 15466937326284535026
|
||||
|
||||
// 在 C++ 中,内置 std:hash() 仅提供基本数据类型的哈希值计算
|
||||
// 数组、对象的哈希值计算需要自行实现
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
=== "Java"
|
||||
|
||||
```javascript title="built_in_hash.js"
|
||||
// JavaScript 未提供内置 hash code 函数
|
||||
```
|
||||
```java title="built_in_hash.java"
|
||||
int num = 3;
|
||||
int hashNum = Integer.hashCode(num);
|
||||
// 整数 3 的哈希值为 3
|
||||
|
||||
=== "TS"
|
||||
boolean bol = true;
|
||||
int hashBol = Boolean.hashCode(bol);
|
||||
// 布尔量 true 的哈希值为 1231
|
||||
|
||||
```typescript title="built_in_hash.ts"
|
||||
// TypeScript 未提供内置 hash code 函数
|
||||
```
|
||||
double dec = 3.14159;
|
||||
int hashDec = Double.hashCode(dec);
|
||||
// 小数 3.14159 的哈希值为 -1340954729
|
||||
|
||||
=== "C"
|
||||
String str = "Hello 算法";
|
||||
int hashStr = str.hashCode();
|
||||
// 字符串 Hello 算法 的哈希值为 -727081396
|
||||
|
||||
```c title="built_in_hash.c"
|
||||
Object[] arr = { 12836, "小哈" };
|
||||
int hashTup = Arrays.hashCode(arr);
|
||||
// 数组 [12836, 小哈] 的哈希值为 1151158
|
||||
|
||||
ListNode obj = new ListNode(0);
|
||||
int hashObj = obj.hashCode();
|
||||
// 节点对象 utils.ListNode@7dc5e7b4 的哈希值为 2110121908
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@ -383,6 +359,12 @@ $$
|
||||
// 节点对象 0 的哈希值为 39053774;
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="built_in_hash.go"
|
||||
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="built_in_hash.swift"
|
||||
@ -411,10 +393,16 @@ $$
|
||||
// 节点对象 utils.ListNode 的哈希值为 -2434780518035996159
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
=== "JS"
|
||||
|
||||
```zig title="built_in_hash.zig"
|
||||
```javascript title="built_in_hash.js"
|
||||
// JavaScript 未提供内置 hash code 函数
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="built_in_hash.ts"
|
||||
// TypeScript 未提供内置 hash code 函数
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
@ -451,6 +439,18 @@ $$
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="built_in_hash.c"
|
||||
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="built_in_hash.zig"
|
||||
|
||||
```
|
||||
|
||||
在许多编程语言中,**只有不可变对象才可作为哈希表的 `key`** 。假如我们将列表(动态数组)作为 `key` ,当列表的内容发生变化时,它的哈希值也随之改变,我们就无法在哈希表中查询到原先的 `value` 了。
|
||||
|
||||
虽然自定义对象(比如链表节点)的成员变量是可变的,但它是可哈希的。**这是因为对象的哈希值通常是基于内存地址生成的**,即使对象的内容发生了变化,但它的内存地址不变,哈希值仍然是不变的。
|
||||
|
||||
Reference in New Issue
Block a user