2023-01-30 15:43:29 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: hashing_search.swift
|
|
|
|
|
* Created Time: 2023-01-28
|
|
|
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import utils
|
|
|
|
|
|
|
|
|
|
/* 哈希查找(数组) */
|
2023-02-04 23:49:37 +08:00
|
|
|
|
func hashingSearchArray(map: [Int: Int], target: Int) -> Int {
|
2023-01-30 15:43:29 +08:00
|
|
|
|
// 哈希表的 key: 目标元素,value: 索引
|
|
|
|
|
// 若哈希表中无此 key ,返回 -1
|
|
|
|
|
return map[target, default: -1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哈希查找(链表) */
|
2023-02-04 23:49:37 +08:00
|
|
|
|
func hashingSearchLinkedList(map: [Int: ListNode], target: Int) -> ListNode? {
|
2023-01-30 15:43:29 +08:00
|
|
|
|
// 哈希表的 key: 目标结点值,value: 结点对象
|
|
|
|
|
// 若哈希表中无此 key ,返回 null
|
|
|
|
|
return map[target]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@main
|
|
|
|
|
enum HashingSearch {
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
static func main() {
|
|
|
|
|
let target = 3
|
|
|
|
|
|
|
|
|
|
/* 哈希查找(数组) */
|
|
|
|
|
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]
|
|
|
|
|
// 初始化哈希表
|
|
|
|
|
var map: [Int: Int] = [:]
|
|
|
|
|
for i in nums.indices {
|
|
|
|
|
map[nums[i]] = i // key: 元素,value: 索引
|
|
|
|
|
}
|
2023-02-04 23:49:37 +08:00
|
|
|
|
let index = hashingSearchArray(map: map, target: target)
|
2023-01-30 15:43:29 +08:00
|
|
|
|
print("目标元素 3 的索引 = \(index)")
|
|
|
|
|
|
|
|
|
|
/* 哈希查找(链表) */
|
|
|
|
|
var head = ListNode.arrToLinkedList(arr: nums)
|
|
|
|
|
// 初始化哈希表
|
|
|
|
|
var map1: [Int: ListNode] = [:]
|
|
|
|
|
while head != nil {
|
|
|
|
|
map1[head!.val] = head! // key: 结点值,value: 结点
|
|
|
|
|
head = head?.next
|
|
|
|
|
}
|
2023-02-04 23:49:37 +08:00
|
|
|
|
let node = hashingSearchLinkedList(map: map1, target: target)
|
2023-01-30 15:43:29 +08:00
|
|
|
|
print("目标结点值 3 的对应结点对象为 \(node!)")
|
|
|
|
|
}
|
|
|
|
|
}
|