From 4355f8d49fd3952b37bf4be5b4a28396b46288c6 Mon Sep 17 00:00:00 2001 From: krahets Date: Wed, 4 Oct 2023 02:30:31 +0800 Subject: [PATCH] Several bug fixes. --- codes/cpp/chapter_hashing/array_hash_map.cpp | 2 +- codes/cpp/chapter_hashing/hash_map_chaining.cpp | 4 ++-- codes/csharp/chapter_hashing/hash_map_chaining.cs | 2 +- codes/csharp/chapter_hashing/hash_map_open_addressing.cs | 2 +- codes/python/chapter_hashing/hash_map_chaining.py | 2 +- docs/chapter_hashing/hash_algorithm.md | 1 - docs/chapter_tree/avl_tree.md | 4 ++-- 7 files changed, 8 insertions(+), 9 deletions(-) diff --git a/codes/cpp/chapter_hashing/array_hash_map.cpp b/codes/cpp/chapter_hashing/array_hash_map.cpp index 30489b67d..6f339d6f1 100644 --- a/codes/cpp/chapter_hashing/array_hash_map.cpp +++ b/codes/cpp/chapter_hashing/array_hash_map.cpp @@ -47,7 +47,7 @@ class ArrayHashMap { int index = hashFunc(key); Pair *pair = buckets[index]; if (pair == nullptr) - return nullptr; + return ""; return pair->val; } diff --git a/codes/cpp/chapter_hashing/hash_map_chaining.cpp b/codes/cpp/chapter_hashing/hash_map_chaining.cpp index 2302f0c0d..1079be379 100644 --- a/codes/cpp/chapter_hashing/hash_map_chaining.cpp +++ b/codes/cpp/chapter_hashing/hash_map_chaining.cpp @@ -50,8 +50,8 @@ class HashMapChaining { return pair->val; } } - // 若未找到 key 则返回 nullptr - return nullptr; + // 若未找到 key 则返回空字符串 + return ""; } /* 添加操作 */ diff --git a/codes/csharp/chapter_hashing/hash_map_chaining.cs b/codes/csharp/chapter_hashing/hash_map_chaining.cs index 39270333a..fec863653 100644 --- a/codes/csharp/chapter_hashing/hash_map_chaining.cs +++ b/codes/csharp/chapter_hashing/hash_map_chaining.cs @@ -37,7 +37,7 @@ class HashMapChaining { } /* 查询操作 */ - public string get(int key) { + public string? get(int key) { int index = hashFunc(key); // 遍历桶,若找到 key 则返回对应 val foreach (Pair pair in buckets[index]) { diff --git a/codes/csharp/chapter_hashing/hash_map_open_addressing.cs b/codes/csharp/chapter_hashing/hash_map_open_addressing.cs index b41a2621d..1e9df4a38 100644 --- a/codes/csharp/chapter_hashing/hash_map_open_addressing.cs +++ b/codes/csharp/chapter_hashing/hash_map_open_addressing.cs @@ -59,7 +59,7 @@ class HashMapOpenAddressing { } /* 查询操作 */ - public string get(int key) { + public string? get(int key) { // 搜索 key 对应的桶索引 int index = findBucket(key); // 若找到键值对,则返回对应 val diff --git a/codes/python/chapter_hashing/hash_map_chaining.py b/codes/python/chapter_hashing/hash_map_chaining.py index 069d43a53..1358650bb 100644 --- a/codes/python/chapter_hashing/hash_map_chaining.py +++ b/codes/python/chapter_hashing/hash_map_chaining.py @@ -29,7 +29,7 @@ class HashMapChaining: """负载因子""" return self.size / self.capacity - def get(self, key: int) -> str: + def get(self, key: int) -> str | None: """查询操作""" index = self.hash_func(key) bucket = self.buckets[index] diff --git a/docs/chapter_hashing/hash_algorithm.md b/docs/chapter_hashing/hash_algorithm.md index e9f8bb2b8..4aac985bf 100644 --- a/docs/chapter_hashing/hash_algorithm.md +++ b/docs/chapter_hashing/hash_algorithm.md @@ -475,7 +475,6 @@ $$ node.borrow().val.hash(&mut hasher); let hash = hasher.finish(); // 节点对象 RefCell { value: ListNode { val: 42, next: None } } 的哈希值为15387811073369036852 - ``` === "C" diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index 89472f22c..8628267ac 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -156,7 +156,7 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉 use std::rc::Rc; use std::cell::RefCell; - /* AVL 树节点类型 */ + /* AVL 树节点结构体 */ struct TreeNode { val: i32, // 节点值 height: i32, // 节点高度 @@ -165,7 +165,7 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉 } impl TreeNode { - /* AVL 树节点构造方法 */ + /* 构造方法 */ fn new(val: i32) -> Rc> { Rc::new(RefCell::new(Self { val,