From c5e5be07b86b7cb337760b1e5f6436dea647f935 Mon Sep 17 00:00:00 2001 From: chenshilong Date: Mon, 12 Dec 2022 16:41:41 +0800 Subject: [PATCH] 1. Hashing search and test using go --- codes/go/chapter_searching/hashing_search.go | 4 +--- docs/chapter_searching/hashing_search.md | 22 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/codes/go/chapter_searching/hashing_search.go b/codes/go/chapter_searching/hashing_search.go index 2920d8e..30096f1 100644 --- a/codes/go/chapter_searching/hashing_search.go +++ b/codes/go/chapter_searching/hashing_search.go @@ -14,7 +14,6 @@ import ( func hashingSearch(m map[int]int, target int) int { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 - if index, ok := m[target]; ok { return index } else { @@ -22,11 +21,10 @@ func hashingSearch(m map[int]int, target int) int { } } -/* 哈希查找(数组) */ +/* 哈希查找(链表) */ func hashingSearch1(m map[int]*pkg.ListNode, target int) *pkg.ListNode { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 nil - if node, ok := m[target]; ok { return node } else { diff --git a/docs/chapter_searching/hashing_search.md b/docs/chapter_searching/hashing_search.md index d00fc21..4fb1898 100644 --- a/docs/chapter_searching/hashing_search.md +++ b/docs/chapter_searching/hashing_search.md @@ -53,7 +53,16 @@ comments: true === "Go" ```go title="hashing_search.go" - + /* 哈希查找(数组) */ + func hashingSearch(m map[int]int, target int) int { + // 哈希表的 key: 目标元素,value: 索引 + // 若哈希表中无此 key ,返回 -1 + if index, ok := m[target]; ok { + return index + } else { + return -1 + } + } ``` === "JavaScript" @@ -121,7 +130,16 @@ comments: true === "Go" ```go title="hashing_search.go" - + /* 哈希查找(链表) */ + func hashingSearch1(m map[int]*pkg.ListNode, target int) *pkg.ListNode { + // 哈希表的 key: 目标结点值,value: 结点对象 + // 若哈希表中无此 key ,返回 nil + if node, ok := m[target]; ok { + return node + } else { + return nil + } + } ``` === "JavaScript"