mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
Squash the language code blocks and fix list.md (#865)
This commit is contained in:
@ -46,149 +46,9 @@ index = hash(key) % capacity
|
||||
- **异或哈希**:将输入数据的每个元素通过异或操作累积到一个哈希值中。
|
||||
- **旋转哈希**:将每个字符的 ASCII 码累积到一个哈希值中,每次累积之前都会对哈希值进行旋转操作。
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="simple_hash.py"
|
||||
[class]{}-[func]{add_hash}
|
||||
|
||||
[class]{}-[func]{mul_hash}
|
||||
|
||||
[class]{}-[func]{xor_hash}
|
||||
|
||||
[class]{}-[func]{rot_hash}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="simple_hash.cpp"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="simple_hash.java"
|
||||
[class]{simple_hash}-[func]{addHash}
|
||||
|
||||
[class]{simple_hash}-[func]{mulHash}
|
||||
|
||||
[class]{simple_hash}-[func]{xorHash}
|
||||
|
||||
[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"
|
||||
|
||||
```go title="simple_hash.go"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[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"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="simple_hash.ts"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="simple_hash.dart"
|
||||
[class]{}-[func]{addHash}
|
||||
|
||||
[class]{}-[func]{mulHash}
|
||||
|
||||
[class]{}-[func]{xorHash}
|
||||
|
||||
[class]{}-[func]{rotHash}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="simple_hash.rs"
|
||||
[class]{}-[func]{add_hash}
|
||||
|
||||
[class]{}-[func]{mul_hash}
|
||||
|
||||
[class]{}-[func]{xor_hash}
|
||||
|
||||
[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}
|
||||
```
|
||||
```src
|
||||
[file]{simple_hash}-[class]{}-[func]{rot_hash}
|
||||
```
|
||||
|
||||
观察发现,每种哈希算法的最后一步都是对大质数 $1000000007$ 取模,以确保哈希值在合适的范围内。值得思考的是,为什么要强调对质数取模,或者说对合数取模的弊端是什么?这是一个有趣的问题。
|
||||
|
||||
|
||||
@ -31,79 +31,9 @@
|
||||
- 使用列表(动态数组)代替链表,从而简化代码。在这种设定下,哈希表(数组)包含多个桶,每个桶都是一个列表。
|
||||
- 以下实现包含哈希表扩容方法。当负载因子超过 $\frac{2}{3}$ 时,我们将哈希表扩容至 $2$ 倍。
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="hash_map_chaining.py"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="hash_map_chaining.cpp"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="hash_map_chaining.java"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="hash_map_chaining.cs"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="hash_map_chaining.go"
|
||||
[class]{hashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="hash_map_chaining.swift"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="hash_map_chaining.js"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="hash_map_chaining.ts"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="hash_map_chaining.dart"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="hash_map_chaining.rs"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="hash_map_chaining.c"
|
||||
[class]{node}-[func]{}
|
||||
|
||||
[class]{hashMapChaining}-[func]{}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="hash_map_chaining.zig"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
```
|
||||
```src
|
||||
[file]{hash_map_chaining}-[class]{hash_map_chaining}-[func]{}
|
||||
```
|
||||
|
||||
值得注意的是,当链表很长时,查询效率 $O(n)$ 很差。**此时可以将链表转换为“AVL 树”或“红黑树”**,从而将查询操作的时间复杂度优化至 $O(\log n)$ 。
|
||||
|
||||
@ -138,77 +68,9 @@
|
||||
|
||||
以下代码实现了一个包含懒删除的开放寻址(线性探测)哈希表。为了更加充分地使用哈希表的空间,我们将哈希表看作是一个“环形数组”,当越过数组尾部时,回到头部继续遍历。
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="hash_map_open_addressing.py"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="hash_map_open_addressing.cpp"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="hash_map_open_addressing.java"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="hash_map_open_addressing.cs"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="hash_map_open_addressing.go"
|
||||
[class]{hashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="hash_map_open_addressing.swift"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="hash_map_open_addressing.js"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="hash_map_open_addressing.ts"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="hash_map_open_addressing.dart"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="hash_map_open_addressing.rs"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="hash_map_open_addressing.c"
|
||||
[class]{hashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="hash_map_open_addressing.zig"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
```
|
||||
```src
|
||||
[file]{hash_map_open_addressing}-[class]{hash_map_open_addressing}-[func]{}
|
||||
```
|
||||
|
||||
### 平方探测
|
||||
|
||||
|
||||
@ -491,110 +491,9 @@ index = hash(key) % capacity
|
||||
|
||||
以下代码实现了一个简单哈希表。其中,我们将 `key` 和 `value` 封装成一个类 `Pair` ,以表示键值对。
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="array_hash_map.py"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="array_hash_map.cpp"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="array_hash_map.java"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="array_hash_map.cs"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="array_hash_map.go"
|
||||
[class]{pair}-[func]{}
|
||||
|
||||
[class]{arrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="array_hash_map.swift"
|
||||
/* 键值对 */
|
||||
class Pair {
|
||||
var key: Int
|
||||
var val: String
|
||||
|
||||
init(key: Int, val: String) {
|
||||
self.key = key
|
||||
self.val = val
|
||||
}
|
||||
}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="array_hash_map.js"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="array_hash_map.ts"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="array_hash_map.dart"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="array_hash_map.rs"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="array_hash_map.c"
|
||||
[class]{pair}-[func]{}
|
||||
|
||||
[class]{arrayHashMap}-[func]{}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="array_hash_map.zig"
|
||||
[class]{Pair}-[func]{}
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
```
|
||||
```src
|
||||
[file]{array_hash_map}-[class]{array_hash_map}-[func]{}
|
||||
```
|
||||
|
||||
## 哈希冲突与扩容
|
||||
|
||||
|
||||
Reference in New Issue
Block a user