2022-12-12 16:36:29 +08:00
|
|
|
|
// File: binary_search.go
|
|
|
|
|
// Created Time: 2022-12-12
|
|
|
|
|
// Author: Slone123c (274325721@qq.com)
|
|
|
|
|
|
|
|
|
|
package chapter_searching
|
|
|
|
|
|
2022-12-12 23:17:33 +08:00
|
|
|
|
import . "github.com/krahets/hello-algo/pkg"
|
2022-12-12 16:36:29 +08:00
|
|
|
|
|
|
|
|
|
/* 哈希查找(数组) */
|
2023-02-04 23:49:37 +08:00
|
|
|
|
func hashingSearchArray(m map[int]int, target int) int {
|
2022-12-12 16:36:29 +08:00
|
|
|
|
// 哈希表的 key: 目标元素,value: 索引
|
|
|
|
|
// 若哈希表中无此 key ,返回 -1
|
|
|
|
|
if index, ok := m[target]; ok {
|
|
|
|
|
return index
|
|
|
|
|
} else {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 16:41:41 +08:00
|
|
|
|
/* 哈希查找(链表) */
|
2023-02-04 23:49:37 +08:00
|
|
|
|
func hashingSearchLinkedList(m map[int]*ListNode, target int) *ListNode {
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 哈希表的 key: 目标节点值,value: 节点对象
|
2022-12-12 16:36:29 +08:00
|
|
|
|
// 若哈希表中无此 key ,返回 nil
|
|
|
|
|
if node, ok := m[target]; ok {
|
|
|
|
|
return node
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|