This commit is contained in:
krahets
2023-05-18 17:17:33 +08:00
parent adfb4e093e
commit 05bbf42d18
3 changed files with 7 additions and 5 deletions

View File

@@ -403,9 +403,11 @@ comments: true
以学生数据 `key 学号 -> value 姓名` 为例,我们可以设计如下哈希函数:
$$
f(x) = x \% 100
f(x) = x \bmod {100}
$$
其中 $\bmod$ 表示取余运算。
![哈希函数工作原理](hash_map.assets/hash_function.png)
<p align="center"> Fig. 哈希函数工作原理 </p>
@@ -1263,7 +1265,7 @@ $$
## 7.1.3. &nbsp; 哈希冲突
细心的你可能已经注意到,**在某些情况下,哈希函数 $f(x) = x % 100$ 可能无法正常工作**。具体来说,当输入的 key 后两位相同时,哈希函数的计算结果也会相同,从而指向同一个 value 。例如,查询学号为 $12836$ 和 $20336$ 的两个学生时,我们得到:
细心的你可能已经注意到,**在某些情况下,哈希函数 $f(x) = x \bmod 100$ 可能无法正常工作**。具体来说,当输入的 key 后两位相同时,哈希函数的计算结果也会相同,从而指向同一个 value 。例如,查询学号为 $12836$ 和 $20336$ 的两个学生时,我们得到:
$$
f(12836) = f(20336) = 36