mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 10:33:34 +08:00
Update the book based on the revised second edition (#1014)
* Revised the book * Update the book with the second revised edition * Revise base on the manuscript of the first edition
This commit is contained in:
@@ -24,7 +24,7 @@ func TestArrayHashMap(t *testing.T) {
|
||||
hmap.print()
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
name := hmap.get(15937)
|
||||
fmt.Println("\n输入学号 15937 ,查询到姓名 " + name)
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestHashMapChaining(t *testing.T) {
|
||||
hmap.print()
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
name := hmap.get(15937)
|
||||
fmt.Println("\n输入学号 15937 ,查询到姓名 ", name)
|
||||
|
||||
@@ -50,7 +50,7 @@ func TestHashMapOpenAddressing(t *testing.T) {
|
||||
hmap.print()
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
name := hmap.get(13276)
|
||||
fmt.Println("\n输入学号 13276 ,查询到姓名 ", name)
|
||||
|
||||
|
||||
@@ -48,13 +48,13 @@ func (m *hashMapChaining) loadFactor() float64 {
|
||||
func (m *hashMapChaining) get(key int) string {
|
||||
idx := m.hashFunc(key)
|
||||
bucket := m.buckets[idx]
|
||||
// 遍历桶,若找到 key 则返回对应 val
|
||||
// 遍历桶,若找到 key ,则返回对应 val
|
||||
for _, p := range bucket {
|
||||
if p.key == key {
|
||||
return p.val
|
||||
}
|
||||
}
|
||||
// 若未找到 key 则返回空字符串
|
||||
// 若未找到 key ,则返回空字符串
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ func (m *hashMapOpenAddressing) get(key int) string {
|
||||
idx := m.hashFunc(key)
|
||||
// 线性探测,从 index 开始向后遍历
|
||||
for i := 0; i < m.capacity; i++ {
|
||||
// 计算桶索引,越过尾部返回头部
|
||||
// 计算桶索引,越过尾部则返回头部
|
||||
j := (idx + i) % m.capacity
|
||||
// 若遇到空桶,说明无此 key ,则返回 null
|
||||
if m.buckets[j] == (pair{}) {
|
||||
@@ -61,7 +61,7 @@ func (m *hashMapOpenAddressing) get(key int) string {
|
||||
return m.buckets[j].val
|
||||
}
|
||||
}
|
||||
// 若未找到 key 则返回空字符串
|
||||
// 若未找到 key ,则返回空字符串
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ func (m *hashMapOpenAddressing) put(key int, val string) {
|
||||
idx := m.hashFunc(key)
|
||||
// 线性探测,从 index 开始向后遍历
|
||||
for i := 0; i < m.capacity; i++ {
|
||||
// 计算桶索引,越过尾部返回头部
|
||||
// 计算桶索引,越过尾部则返回头部
|
||||
j := (idx + i) % m.capacity
|
||||
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
|
||||
if m.buckets[j] == (pair{}) || m.buckets[j] == m.removed {
|
||||
@@ -99,7 +99,7 @@ func (m *hashMapOpenAddressing) remove(key int) {
|
||||
// 遍历桶,从中删除键值对
|
||||
// 线性探测,从 index 开始向后遍历
|
||||
for i := 0; i < m.capacity; i++ {
|
||||
// 计算桶索引,越过尾部返回头部
|
||||
// 计算桶索引,越过尾部则返回头部
|
||||
j := (idx + i) % m.capacity
|
||||
// 若遇到空桶,说明无此 key ,则直接返回
|
||||
if m.buckets[j] == (pair{}) {
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestHashMap(t *testing.T) {
|
||||
PrintMap(hmap)
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
name := hmap[15937]
|
||||
fmt.Println("\n输入学号 15937 ,查询到姓名 ", name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user