mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-17 22:10:59 +00:00
build
This commit is contained in:
@@ -601,13 +601,45 @@ index = hash(key) % capacity
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="simple_hash.rb"
|
||||
[class]{}-[func]{add_hash}
|
||||
### 加法雜湊 ###
|
||||
def add_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
[class]{}-[func]{mul_hash}
|
||||
key.each_char { |c| hash += c.ord }
|
||||
|
||||
[class]{}-[func]{xor_hash}
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
[class]{}-[func]{rot_hash}
|
||||
### 乘法雜湊 ###
|
||||
def mul_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
key.each_char { |c| hash = 31 * hash + c.ord }
|
||||
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
### 互斥或雜湊 ###
|
||||
def xor_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
key.each_char { |c| hash ^= c.ord }
|
||||
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
### 旋轉雜湊 ###
|
||||
def rot_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
key.each_char { |c| hash = (hash << 4) ^ (hash >> 28) ^ c.ord }
|
||||
|
||||
hash % modulus
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -880,7 +912,7 @@ $$
|
||||
```rust title="built_in_hash.rs"
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
|
||||
let num = 3;
|
||||
let mut num_hasher = DefaultHasher::new();
|
||||
num.hash(&mut num_hasher);
|
||||
@@ -955,7 +987,29 @@ $$
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="built_in_hash.rb"
|
||||
num = 3
|
||||
hash_num = num.hash
|
||||
# 整數 3 的雜湊值為 -4385856518450339636
|
||||
|
||||
bol = true
|
||||
hash_bol = bol.hash
|
||||
# 布林量 true 的雜湊值為 -1617938112149317027
|
||||
|
||||
dec = 3.14159
|
||||
hash_dec = dec.hash
|
||||
# 小數 3.14159 的雜湊值為 -1479186995943067893
|
||||
|
||||
str = "Hello 演算法"
|
||||
hash_str = str.hash
|
||||
# 字串“Hello 演算法”的雜湊值為 -4075943250025831763
|
||||
|
||||
tup = [12836, '小哈']
|
||||
hash_tup = tup.hash
|
||||
# 元組 (12836, '小哈') 的雜湊值為 1999544809202288822
|
||||
|
||||
obj = ListNode.new(0)
|
||||
hash_obj = obj.hash
|
||||
# 節點物件 #<ListNode:0x000078133140ab70> 的雜湊值為 4302940560806366381
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
@@ -1426,7 +1426,99 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="hash_map_chaining.rb"
|
||||
[class]{HashMapChaining}-[func]{}
|
||||
### 鍵式位址雜湊表 ###
|
||||
class HashMapChaining
|
||||
### 建構子 ###
|
||||
def initialize
|
||||
@size = 0 # 鍵值對數量
|
||||
@capacity = 4 # 雜湊表容量
|
||||
@load_thres = 2.0 / 3.0 # 觸發擴容的負載因子閾值
|
||||
@extend_ratio = 2 # 擴容倍數
|
||||
@buckets = Array.new(@capacity) { [] } # 桶陣列
|
||||
end
|
||||
|
||||
### 雜湊函式 ###
|
||||
def hash_func(key)
|
||||
key % @capacity
|
||||
end
|
||||
|
||||
### 負載因子 ###
|
||||
def load_factor
|
||||
@size / @capacity
|
||||
end
|
||||
|
||||
### 查詢操作 ###
|
||||
def get(key)
|
||||
index = hash_func(key)
|
||||
bucket = @buckets[index]
|
||||
# 走訪桶,若找到 key ,則返回對應 val
|
||||
for pair in bucket
|
||||
return pair.val if pair.key == key
|
||||
end
|
||||
# 若未找到 key , 則返回 nil
|
||||
nil
|
||||
end
|
||||
|
||||
### 新增操作 ###
|
||||
def put(key, val)
|
||||
# 當負載因子超過閾值時,執行擴容
|
||||
extend if load_factor > @load_thres
|
||||
index = hash_func(key)
|
||||
bucket = @buckets[index]
|
||||
# 走訪桶,若遇到指定 key ,則更新對應 val 並返回
|
||||
for pair in bucket
|
||||
if pair.key == key
|
||||
pair.val = val
|
||||
return
|
||||
end
|
||||
end
|
||||
# 若無該 key ,則將鍵值對新增至尾部
|
||||
pair = Pair.new(key, val)
|
||||
bucket << pair
|
||||
@size += 1
|
||||
end
|
||||
|
||||
### 刪除操作 ###
|
||||
def remove(key)
|
||||
index = hash_func(key)
|
||||
bucket = @buckets[index]
|
||||
# 走訪桶,從中刪除鍵值對
|
||||
for pair in bucket
|
||||
if pair.key == key
|
||||
bucket.delete(pair)
|
||||
@size -= 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
### 擴容雜湊表 ###
|
||||
def extend
|
||||
# 暫存原雜湊表
|
||||
buckets = @buckets
|
||||
# 初始化擴容後的新雜湊表
|
||||
@capacity *= @extend_ratio
|
||||
@buckets = Array.new(@capacity) { [] }
|
||||
@size = 0
|
||||
# 將鍵值對從原雜湊表搬運至新雜湊表
|
||||
for bucket in buckets
|
||||
for pair in bucket
|
||||
put(pair.key, pair.val)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
### 列印雜湊表 ###
|
||||
def print
|
||||
for bucket in @buckets
|
||||
res = []
|
||||
for pair in bucket
|
||||
res << "#{pair.key} -> #{pair.val}"
|
||||
end
|
||||
pp res
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -3086,7 +3178,118 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="hash_map_open_addressing.rb"
|
||||
[class]{HashMapOpenAddressing}-[func]{}
|
||||
### 開放定址雜湊表 ###
|
||||
class HashMapOpenAddressing
|
||||
TOMBSTONE = Pair.new(-1, '-1') # 刪除標記
|
||||
|
||||
### 建構子 ###
|
||||
def initialize
|
||||
@size = 0 # 鍵值對數量
|
||||
@capacity = 4 # 雜湊表容量
|
||||
@load_thres = 2.0 / 3.0 # 觸發擴容的負載因子閾值
|
||||
@extend_ratio = 2 # 擴容倍數
|
||||
@buckets = Array.new(@capacity) # 桶陣列
|
||||
end
|
||||
|
||||
### 雜湊函式 ###
|
||||
def hash_func(key)
|
||||
key % @capacity
|
||||
end
|
||||
|
||||
### 負載因子 ###
|
||||
def load_factor
|
||||
@size / @capacity
|
||||
end
|
||||
|
||||
### 搜尋 key 對應的桶索引 ###
|
||||
def find_bucket(key)
|
||||
index = hash_func(key)
|
||||
first_tombstone = -1
|
||||
# 線性探查,當遇到空桶時跳出
|
||||
while !@buckets[index].nil?
|
||||
# 若遇到 key ,返回對應的桶索引
|
||||
if @buckets[index].key == key
|
||||
# 若之前遇到了刪除標記,則將鍵值對移動至該索引處
|
||||
if first_tombstone != -1
|
||||
@buckets[first_tombstone] = @buckets[index]
|
||||
@buckets[index] = TOMBSTONE
|
||||
return first_tombstone # 返回移動後的桶索引
|
||||
end
|
||||
return index # 返回桶索引
|
||||
end
|
||||
# 記錄遇到的首個刪除標記
|
||||
first_tombstone = index if first_tombstone == -1 && @buckets[index] == TOMBSTONE
|
||||
# 計算桶索引,越過尾部則返回頭部
|
||||
index = (index + 1) % @capacity
|
||||
end
|
||||
# 若 key 不存在,則返回新增點的索引
|
||||
first_tombstone == -1 ? index : first_tombstone
|
||||
end
|
||||
|
||||
### 查詢操作 ###
|
||||
def get(key)
|
||||
# 搜尋 key 對應的桶索引
|
||||
index = find_bucket(key)
|
||||
# 若找到鍵值對,則返回對應 val
|
||||
return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])
|
||||
# 若鍵值對不存在,則返回 nil
|
||||
nil
|
||||
end
|
||||
|
||||
### 新增操作 ###
|
||||
def put(key, val)
|
||||
# 當負載因子超過閾值時,執行擴容
|
||||
extend if load_factor > @load_thres
|
||||
# 搜尋 key 對應的桶索引
|
||||
index = find_bucket(key)
|
||||
# 若找到鍵值對,則覆蓋 val 開返回
|
||||
unless [nil, TOMBSTONE].include?(@buckets[index])
|
||||
@buckets[index].val = val
|
||||
return
|
||||
end
|
||||
# 若鍵值對不存在,則新增該鍵值對
|
||||
@buckets[index] = Pair.new(key, val)
|
||||
@size += 1
|
||||
end
|
||||
|
||||
### 刪除操作 ###
|
||||
def remove(key)
|
||||
# 搜尋 key 對應的桶索引
|
||||
index = find_bucket(key)
|
||||
# 若找到鍵值對,則用刪除標記覆蓋它
|
||||
unless [nil, TOMBSTONE].include?(@buckets[index])
|
||||
@buckets[index] = TOMBSTONE
|
||||
@size -= 1
|
||||
end
|
||||
end
|
||||
|
||||
### 擴容雜湊表 ###
|
||||
def extend
|
||||
# 暫存原雜湊表
|
||||
buckets_tmp = @buckets
|
||||
# 初始化擴容後的新雜湊表
|
||||
@capacity *= @extend_ratio
|
||||
@buckets = Array.new(@capacity)
|
||||
@size = 0
|
||||
# 將鍵值對從原雜湊表搬運至新雜湊表
|
||||
for pair in buckets_tmp
|
||||
put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)
|
||||
end
|
||||
end
|
||||
|
||||
### 列印雜湊表 ###
|
||||
def print
|
||||
for pair in @buckets
|
||||
if pair.nil?
|
||||
puts "Nil"
|
||||
elsif pair == TOMBSTONE
|
||||
puts "TOMBSTONE"
|
||||
else
|
||||
puts "#{pair.key} -> #{pair.val}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
@@ -41,7 +41,7 @@ comments: true
|
||||
```python title="hash_map.py"
|
||||
# 初始化雜湊表
|
||||
hmap: dict = {}
|
||||
|
||||
|
||||
# 新增操作
|
||||
# 在雜湊表中新增鍵值對 (key, value)
|
||||
hmap[12836] = "小哈"
|
||||
@@ -49,11 +49,11 @@ comments: true
|
||||
hmap[16750] = "小算"
|
||||
hmap[13276] = "小法"
|
||||
hmap[10583] = "小鴨"
|
||||
|
||||
|
||||
# 查詢操作
|
||||
# 向雜湊表中輸入鍵 key ,得到值 value
|
||||
name: str = hmap[15937]
|
||||
|
||||
|
||||
# 刪除操作
|
||||
# 在雜湊表中刪除鍵值對 (key, value)
|
||||
hmap.pop(10583)
|
||||
@@ -64,7 +64,7 @@ comments: true
|
||||
```cpp title="hash_map.cpp"
|
||||
/* 初始化雜湊表 */
|
||||
unordered_map<int, string> map;
|
||||
|
||||
|
||||
/* 新增操作 */
|
||||
// 在雜湊表中新增鍵值對 (key, value)
|
||||
map[12836] = "小哈";
|
||||
@@ -72,11 +72,11 @@ comments: true
|
||||
map[16750] = "小算";
|
||||
map[13276] = "小法";
|
||||
map[10583] = "小鴨";
|
||||
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
string name = map[15937];
|
||||
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
map.erase(10583);
|
||||
@@ -87,19 +87,19 @@ comments: true
|
||||
```java title="hash_map.java"
|
||||
/* 初始化雜湊表 */
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
|
||||
|
||||
/* 新增操作 */
|
||||
// 在雜湊表中新增鍵值對 (key, value)
|
||||
map.put(12836, "小哈");
|
||||
map.put(15937, "小囉");
|
||||
map.put(16750, "小算");
|
||||
map.put(12836, "小哈");
|
||||
map.put(15937, "小囉");
|
||||
map.put(16750, "小算");
|
||||
map.put(13276, "小法");
|
||||
map.put(10583, "小鴨");
|
||||
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
String name = map.get(15937);
|
||||
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
map.remove(10583);
|
||||
@@ -118,11 +118,11 @@ comments: true
|
||||
{ 13276, "小法" },
|
||||
{ 10583, "小鴨" }
|
||||
};
|
||||
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
string name = map[15937];
|
||||
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
map.Remove(10583);
|
||||
@@ -133,7 +133,7 @@ comments: true
|
||||
```go title="hash_map_test.go"
|
||||
/* 初始化雜湊表 */
|
||||
hmap := make(map[int]string)
|
||||
|
||||
|
||||
/* 新增操作 */
|
||||
// 在雜湊表中新增鍵值對 (key, value)
|
||||
hmap[12836] = "小哈"
|
||||
@@ -141,11 +141,11 @@ comments: true
|
||||
hmap[16750] = "小算"
|
||||
hmap[13276] = "小法"
|
||||
hmap[10583] = "小鴨"
|
||||
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
name := hmap[15937]
|
||||
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
delete(hmap, 10583)
|
||||
@@ -156,7 +156,7 @@ comments: true
|
||||
```swift title="hash_map.swift"
|
||||
/* 初始化雜湊表 */
|
||||
var map: [Int: String] = [:]
|
||||
|
||||
|
||||
/* 新增操作 */
|
||||
// 在雜湊表中新增鍵值對 (key, value)
|
||||
map[12836] = "小哈"
|
||||
@@ -164,11 +164,11 @@ comments: true
|
||||
map[16750] = "小算"
|
||||
map[13276] = "小法"
|
||||
map[10583] = "小鴨"
|
||||
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
let name = map[15937]!
|
||||
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
map.removeValue(forKey: 10583)
|
||||
@@ -186,11 +186,11 @@ comments: true
|
||||
map.set(16750, '小算');
|
||||
map.set(13276, '小法');
|
||||
map.set(10583, '小鴨');
|
||||
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
let name = map.get(15937);
|
||||
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
map.delete(10583);
|
||||
@@ -210,12 +210,12 @@ comments: true
|
||||
map.set(10583, '小鴨');
|
||||
console.info('\n新增完成後,雜湊表為\nKey -> Value');
|
||||
console.info(map);
|
||||
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
let name = map.get(15937);
|
||||
console.info('\n輸入學號 15937 ,查詢到姓名 ' + name);
|
||||
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
map.delete(10583);
|
||||
@@ -250,7 +250,7 @@ comments: true
|
||||
|
||||
```rust title="hash_map.rs"
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
||||
/* 初始化雜湊表 */
|
||||
let mut map: HashMap<i32, String> = HashMap::new();
|
||||
|
||||
@@ -282,7 +282,7 @@ comments: true
|
||||
```kotlin title="hash_map.kt"
|
||||
/* 初始化雜湊表 */
|
||||
val map = HashMap<Int,String>()
|
||||
|
||||
|
||||
/* 新增操作 */
|
||||
// 在雜湊表中新增鍵值對 (key, value)
|
||||
map[12836] = "小哈"
|
||||
@@ -290,11 +290,11 @@ comments: true
|
||||
map[16750] = "小算"
|
||||
map[13276] = "小法"
|
||||
map[10583] = "小鴨"
|
||||
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
val name = map[15937]
|
||||
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
map.remove(10583)
|
||||
@@ -303,7 +303,24 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="hash_map.rb"
|
||||
# 初始化雜湊表
|
||||
hmap = {}
|
||||
|
||||
# 新增操作
|
||||
# 在雜湊表中新增鍵值對 (key, value)
|
||||
hmap[12836] = "小哈"
|
||||
hmap[15937] = "小囉"
|
||||
hmap[16750] = "小算"
|
||||
hmap[13276] = "小法"
|
||||
hmap[10583] = "小鴨"
|
||||
|
||||
# 查詢操作
|
||||
# 向雜湊表中輸入鍵 key ,得到值 value
|
||||
name = hmap[15937]
|
||||
|
||||
# 刪除操作
|
||||
# 在雜湊表中刪除鍵值對 (key, value)
|
||||
hmap.delete(10583)
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -487,7 +504,7 @@ comments: true
|
||||
|
||||
// 單獨走訪鍵 Key
|
||||
for key in map.keys() {
|
||||
println!("{key}");
|
||||
println!("{key}");
|
||||
}
|
||||
|
||||
// 單獨走訪值 Value
|
||||
@@ -523,7 +540,15 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="hash_map.rb"
|
||||
# 走訪雜湊表
|
||||
# 走訪鍵值對 key->value
|
||||
hmap.entries.each { |key, value| puts "#{key} -> #{value}" }
|
||||
|
||||
# 單獨走訪鍵 key
|
||||
hmap.keys.each { |key| puts key }
|
||||
|
||||
# 單獨走訪值 value
|
||||
hmap.values.each { |val| puts val }
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -1666,9 +1691,78 @@ index = hash(key) % capacity
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array_hash_map.rb"
|
||||
[class]{Pair}-[func]{}
|
||||
### 鍵值對 ###
|
||||
class Pair
|
||||
attr_accessor :key, :val
|
||||
|
||||
[class]{ArrayHashMap}-[func]{}
|
||||
def initialize(key, val)
|
||||
@key = key
|
||||
@val = val
|
||||
end
|
||||
end
|
||||
|
||||
### 基於陣列實現的雜湊表 ###
|
||||
class ArrayHashMap
|
||||
### 建構子 ###
|
||||
def initialize
|
||||
# 初始化陣列,包含 100 個桶
|
||||
@buckets = Array.new(100)
|
||||
end
|
||||
|
||||
### 雜湊函式 ###
|
||||
def hash_func(key)
|
||||
index = key % 100
|
||||
end
|
||||
|
||||
### 查詢操作 ###
|
||||
def get(key)
|
||||
index = hash_func(key)
|
||||
pair = @buckets[index]
|
||||
|
||||
return if pair.nil?
|
||||
pair.val
|
||||
end
|
||||
|
||||
### 新增操作 ###
|
||||
def put(key, val)
|
||||
pair = Pair.new(key, val)
|
||||
index = hash_func(key)
|
||||
@buckets[index] = pair
|
||||
end
|
||||
|
||||
### 刪除操作 ###
|
||||
def remove(key)
|
||||
index = hash_func(key)
|
||||
# 置為 nil ,代表刪除
|
||||
@buckets[index] = nil
|
||||
end
|
||||
|
||||
### 獲取所有鍵值對 ###
|
||||
def entry_set
|
||||
result = []
|
||||
@buckets.each { |pair| result << pair unless pair.nil? }
|
||||
result
|
||||
end
|
||||
|
||||
### 獲取所有鍵 ###
|
||||
def key_set
|
||||
result = []
|
||||
@buckets.each { |pair| result << pair.key unless pair.nil? }
|
||||
result
|
||||
end
|
||||
|
||||
### 獲取所有值 ###
|
||||
def value_set
|
||||
result = []
|
||||
@buckets.each { |pair| result << pair.val unless pair.nil? }
|
||||
result
|
||||
end
|
||||
|
||||
### 列印雜湊表 ###
|
||||
def print
|
||||
@buckets.each { |pair| puts "#{pair.key} -> #{pair.val}" unless pair.nil? }
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
Reference in New Issue
Block a user