hello-algo/codes/go/chapter_searching/hashing_search.go

30 lines
695 B
Go
Raw Normal View History

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