From d0f4fa69a6d42efc0b4c9b6c569f7a52cb462155 Mon Sep 17 00:00:00 2001 From: Reanon <793584285@qq.com> Date: Sat, 16 Dec 2023 18:52:28 +0800 Subject: [PATCH] fix(go): fix go code , refer to @joengtou @wcig @shenjq (#992) --- .../chapter_backtracking/preorder_traversal_ii_compact.go | 2 +- codes/go/chapter_hashing/hash_map_chaining.go | 8 ++++---- codes/go/chapter_hashing/hash_map_open_addressing.go | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/codes/go/chapter_backtracking/preorder_traversal_ii_compact.go b/codes/go/chapter_backtracking/preorder_traversal_ii_compact.go index d59bfa96c..b6064412c 100644 --- a/codes/go/chapter_backtracking/preorder_traversal_ii_compact.go +++ b/codes/go/chapter_backtracking/preorder_traversal_ii_compact.go @@ -17,7 +17,7 @@ func preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) { *path = append(*path, root) if root.Val.(int) == 7 { // 记录解 - *res = append(*res, *path) + *res = append(*res, append([]*TreeNode{}, *path...)) } preOrderII(root.Left, res, path) preOrderII(root.Right, res, path) diff --git a/codes/go/chapter_hashing/hash_map_chaining.go b/codes/go/chapter_hashing/hash_map_chaining.go index 201674090..e7f17416c 100644 --- a/codes/go/chapter_hashing/hash_map_chaining.go +++ b/codes/go/chapter_hashing/hash_map_chaining.go @@ -41,7 +41,7 @@ func (m *hashMapChaining) hashFunc(key int) int { /* 负载因子 */ func (m *hashMapChaining) loadFactor() float64 { - return float64(m.size / m.capacity) + return float64(m.size) / float64(m.capacity) } /* 查询操作 */ @@ -66,9 +66,9 @@ func (m *hashMapChaining) put(key int, val string) { } idx := m.hashFunc(key) // 遍历桶,若遇到指定 key ,则更新对应 val 并返回 - for _, p := range m.buckets[idx] { - if p.key == key { - p.val = val + for i := range m.buckets[idx] { + if m.buckets[idx][i].key == key { + m.buckets[idx][i].val = val return } } diff --git a/codes/go/chapter_hashing/hash_map_open_addressing.go b/codes/go/chapter_hashing/hash_map_open_addressing.go index 5a5dfed1b..c73be22ae 100644 --- a/codes/go/chapter_hashing/hash_map_open_addressing.go +++ b/codes/go/chapter_hashing/hash_map_open_addressing.go @@ -88,6 +88,7 @@ func (m *hashMapOpenAddressing) put(key int, val string) { // 若遇到指定 key ,则更新对应 val if m.buckets[j].key == key { m.buckets[j].val = val + return } } }