mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-13 23:56:07 +00:00
build
This commit is contained in:
@@ -24,15 +24,15 @@ comments: true
|
||||
|
||||
**第一步:思考每轮的决策,定义状态,从而得到 $dp$ 表**
|
||||
|
||||
对于每个物品来说,不放入背包,背包容量不变;放入背包,背包容量减小。由此可得状态定义:当前物品编号 $i$ 和剩余背包容量 $c$ ,记为 $[i, c]$ 。
|
||||
对于每个物品来说,不放入背包,背包容量不变;放入背包,背包容量减小。由此可得状态定义:当前物品编号 $i$ 和背包容量 $c$ ,记为 $[i, c]$ 。
|
||||
|
||||
状态 $[i, c]$ 对应的子问题为:**前 $i$ 个物品在剩余容量为 $c$ 的背包中的最大价值**,记为 $dp[i, c]$ 。
|
||||
状态 $[i, c]$ 对应的子问题为:**前 $i$ 个物品在容量为 $c$ 的背包中的最大价值**,记为 $dp[i, c]$ 。
|
||||
|
||||
待求解的是 $dp[n, cap]$ ,因此需要一个尺寸为 $(n+1) \times (cap+1)$ 的二维 $dp$ 表。
|
||||
|
||||
**第二步:找出最优子结构,进而推导出状态转移方程**
|
||||
|
||||
当我们做出物品 $i$ 的决策后,剩余的是前 $i-1$ 个物品的决策,可分为以下两种情况。
|
||||
当我们做出物品 $i$ 的决策后,剩余的是前 $i-1$ 个物品决策的子问题,可分为以下两种情况。
|
||||
|
||||
- **不放入物品 $i$** :背包容量不变,状态变化为 $[i-1, c]$ 。
|
||||
- **放入物品 $i$** :背包容量减少 $wgt[i-1]$ ,价值增加 $val[i-1]$ ,状态变化为 $[i-1, c-wgt[i-1]]$ 。
|
||||
@@ -47,7 +47,7 @@ $$
|
||||
|
||||
**第三步:确定边界条件和状态转移顺序**
|
||||
|
||||
当无物品或无剩余背包容量时最大价值为 $0$ ,即首列 $dp[i, 0]$ 和首行 $dp[0, c]$ 都等于 $0$ 。
|
||||
当无物品或背包容量为 $0$ 时最大价值为 $0$ ,即首列 $dp[i, 0]$ 和首行 $dp[0, c]$ 都等于 $0$ 。
|
||||
|
||||
当前状态 $[i, c]$ 从上方的状态 $[i-1, c]$ 和左上方的状态 $[i-1, c-wgt[i-1]]$ 转移而来,因此通过两层循环正序遍历整个 $dp$ 表即可。
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ comments: true
|
||||
**背包问题**
|
||||
|
||||
- 背包问题是最典型的动态规划问题之一,具有 0-1 背包、完全背包、多重背包等变种。
|
||||
- 0-1 背包的状态定义为前 $i$ 个物品在剩余容量为 $c$ 的背包中的最大价值。根据不放入背包和放入背包两种决策,可得到最优子结构,并构建出状态转移方程。在空间优化中,由于每个状态依赖正上方和左上方的状态,因此需要倒序遍历列表,避免左上方状态被覆盖。
|
||||
- 0-1 背包的状态定义为前 $i$ 个物品在容量为 $c$ 的背包中的最大价值。根据不放入背包和放入背包两种决策,可得到最优子结构,并构建出状态转移方程。在空间优化中,由于每个状态依赖正上方和左上方的状态,因此需要倒序遍历列表,避免左上方状态被覆盖。
|
||||
- 完全背包问题的每种物品的选取数量无限制,因此选择放入物品的状态转移与 0-1 背包问题不同。由于状态依赖正上方和正左方的状态,因此在空间优化中应当正序遍历。
|
||||
- 零钱兑换问题是完全背包问题的一个变种。它从求“最大”价值变为求“最小”硬币数量,因此状态转移方程中的 $\max()$ 应改为 $\min()$ 。从追求“不超过”背包容量到追求“恰好”凑出目标金额,因此使用 $amt + 1$ 来表示“无法凑出目标金额”的无效解。
|
||||
- 零钱兑换问题 II 从求“最少硬币数量”改为求“硬币组合数量”,状态转移方程相应地从 $\min()$ 改为求和运算符。
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -462,7 +462,7 @@ comments: true
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""判断双向队列是否为空"""
|
||||
return self.size() == 0
|
||||
return self._size == 0
|
||||
|
||||
def push(self, num: int, is_front: bool):
|
||||
"""入队操作"""
|
||||
|
||||
@@ -416,7 +416,7 @@ comments: true
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""判断队列是否为空"""
|
||||
return not self._front
|
||||
return self._size == 0
|
||||
|
||||
def push(self, num: int):
|
||||
"""入队"""
|
||||
|
||||
@@ -412,7 +412,7 @@ comments: true
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""判断栈是否为空"""
|
||||
return not self._peek
|
||||
return self._size == 0
|
||||
|
||||
def push(self, val: int):
|
||||
"""入栈"""
|
||||
@@ -1284,7 +1284,7 @@ comments: true
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""判断栈是否为空"""
|
||||
return self._stack == []
|
||||
return self._size == 0
|
||||
|
||||
def push(self, item: int):
|
||||
"""入栈"""
|
||||
|
||||
Reference in New Issue
Block a user