Add Swift language blocks to the docs.

This commit is contained in:
Yudong Jin
2023-01-08 19:41:05 +08:00
parent 3ba37dba3a
commit 73e3452838
22 changed files with 414 additions and 70 deletions

View File

@@ -108,25 +108,25 @@ comments: true
=== "Go"
```go title="hash_map_test.go"
/* 初始化哈希表 */
mapp := make(map[int]string)
```go title="hash_map.go"
/* 初始化哈希表 */
mapp := make(map[int]string)
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
mapp[12836] = "小哈"
mapp[15937] = "小啰"
mapp[16750] = "小算"
mapp[13276] = "小法"
mapp[10583] = "小鸭"
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
mapp[12836] = "小哈"
mapp[15937] = "小啰"
mapp[16750] = "小算"
mapp[13276] = "小法"
mapp[10583] = "小鸭"
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
name := mapp[15937]
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
name := mapp[15937]
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
delete(mapp, 10583)
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
delete(mapp, 10583)
```
=== "JavaScript"
@@ -207,6 +207,12 @@ comments: true
map.Remove(10583);
```
=== "Swift"
```swift title="hash_map.swift"
```
遍历哈希表有三种方式,即 **遍历键值对、遍历键、遍历值**。
=== "Java"
@@ -339,6 +345,12 @@ comments: true
}
```
=== "Swift"
```swift title="hash_map.swift"
```
## 哈希函数
哈希表中存储元素的数据结构被称为「桶 Bucket」底层实现可能是数组、链表、二叉树红黑树或是它们的组合。
@@ -756,6 +768,12 @@ $$
}
```
=== "Swift"
```swift title="array_hash_map.swift"
```
## 哈希冲突
细心的同学可能会发现,**哈希函数 $f(x) = x \% 100$ 会在某些情况下失效**。具体地,当输入的 key 后两位相同时,哈希函数的计算结果也相同,指向同一个 value 。例如,分别查询两个学号 12836 和 20336 ,则有