mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-24 09:07:14 +00:00
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:
@@ -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]
|
||||
}
|
||||
```
|
||||
|
||||
## 复杂度分析
|
||||
|
||||
Reference in New Issue
Block a user