feat: add Swift codes for chapter_searching articles (#309)

* feat: add Swift codes for linear_search article

* feat: add Swift codes for binary_search article

* feat: add Swift codes for hashing_search article
This commit is contained in:
nuomi1
2023-01-30 15:43:29 +08:00
committed by GitHub
parent 15c798046a
commit 1665fe176c
8 changed files with 261 additions and 7 deletions
+12 -2
View File
@@ -108,7 +108,12 @@ comments: true
=== "Swift"
```swift title="hashing_search.swift"
/* 哈希查找(数组) */
func hashingSearch(map: [Int: Int], target: Int) -> Int {
// 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1
return map[target, default: -1]
}
```
再比如,如果我们想要给定一个目标结点值 `target` ,获取对应的链表结点对象,那么也可以使用哈希查找实现。
@@ -208,7 +213,12 @@ comments: true
=== "Swift"
```swift title="hashing_search.swift"
/* 哈希查找(链表) */
func hashingSearch1(map: [Int: ListNode], target: Int) -> ListNode? {
// 哈希表的 key: 目标结点值,value: 结点对象
// 若哈希表中无此 key ,返回 null
return map[target]
}
```
## 复杂度分析