Several bug fixes.

pull/793/head
krahets 1 year ago
parent e3773b7f76
commit ff8e7ceec5

@ -48,7 +48,7 @@ hashMapChaining *newHashMapChaining() {
hashmap->capacity = tableSize; hashmap->capacity = tableSize;
hashmap->size = 0; hashmap->size = 0;
hashmap->extendRatio = 2; hashmap->extendRatio = 2;
hashmap->loadThres = 2.0 / 3; hashmap->loadThres = 2.0 / 3.0;
return hashmap; return hashmap;
} }

@ -17,7 +17,7 @@ class HashMapChaining {
public: public:
/* 构造方法 */ /* 构造方法 */
HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3), extendRatio(2) { HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {
buckets.resize(capacity); buckets.resize(capacity);
} }

@ -11,7 +11,7 @@ class HashMapOpenAddressing {
private: private:
int size; // 键值对数量 int size; // 键值对数量
int capacity = 4; // 哈希表容量 int capacity = 4; // 哈希表容量
const double loadThres = 2.0 / 3; // 触发扩容的负载因子阈值 const double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
const int extendRatio = 2; // 扩容倍数 const int extendRatio = 2; // 扩容倍数
vector<Pair *> buckets; // 桶数组 vector<Pair *> buckets; // 桶数组
Pair *TOMBSTONE = new Pair(-1, "-1"); // 删除标记 Pair *TOMBSTONE = new Pair(-1, "-1"); // 删除标记

@ -18,7 +18,7 @@ class HashMapChaining {
public HashMapChaining() { public HashMapChaining() {
size = 0; size = 0;
capacity = 4; capacity = 4;
loadThres = 2 / 3.0; loadThres = 2.0 / 3.0;
extendRatio = 2; extendRatio = 2;
buckets = new List<List<Pair>>(capacity); buckets = new List<List<Pair>>(capacity);
for (int i = 0; i < capacity; i++) { for (int i = 0; i < capacity; i++) {

@ -10,7 +10,7 @@ namespace hello_algo.chapter_hashing;
class HashMapOpenAddressing { class HashMapOpenAddressing {
private int size; // 键值对数量 private int size; // 键值对数量
private int capacity = 4; // 哈希表容量 private int capacity = 4; // 哈希表容量
private double loadThres = 2.0 / 3; // 触发扩容的负载因子阈值 private double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
private int extendRatio = 2; // 扩容倍数 private int extendRatio = 2; // 扩容倍数
private Pair[] buckets; // 桶数组 private Pair[] buckets; // 桶数组
private Pair TOMBSTONE = new Pair(-1, "-1"); // 删除标记 private Pair TOMBSTONE = new Pair(-1, "-1"); // 删除标记

@ -23,7 +23,7 @@ class HashMapChaining {
HashMapChaining() { HashMapChaining() {
size = 0; size = 0;
capacity = 4; capacity = 4;
loadThres = 2 / 3.0; loadThres = 2.0 / 3.0;
extendRatio = 2; extendRatio = 2;
buckets = List.generate(capacity, (_) => []); buckets = List.generate(capacity, (_) => []);
} }

@ -28,7 +28,7 @@ func newHashMapChaining() *hashMapChaining {
return &hashMapChaining{ return &hashMapChaining{
size: 0, size: 0,
capacity: 4, capacity: 4,
loadThres: 2 / 3.0, loadThres: 2.0 / 3.0,
extendRatio: 2, extendRatio: 2,
buckets: buckets, buckets: buckets,
} }

@ -25,7 +25,7 @@ func newHashMapOpenAddressing() *hashMapOpenAddressing {
return &hashMapOpenAddressing{ return &hashMapOpenAddressing{
size: 0, size: 0,
capacity: 4, capacity: 4,
loadThres: 2 / 3.0, loadThres: 2.0 / 3.0,
extendRatio: 2, extendRatio: 2,
buckets: buckets, buckets: buckets,
removed: pair{ removed: pair{

@ -21,7 +21,7 @@ class HashMapChaining {
public HashMapChaining() { public HashMapChaining() {
size = 0; size = 0;
capacity = 4; capacity = 4;
loadThres = 2 / 3.0; loadThres = 2.0 / 3.0;
extendRatio = 2; extendRatio = 2;
buckets = new ArrayList<>(capacity); buckets = new ArrayList<>(capacity);
for (int i = 0; i < capacity; i++) { for (int i = 0; i < capacity; i++) {

@ -10,7 +10,7 @@ package chapter_hashing;
class HashMapOpenAddressing { class HashMapOpenAddressing {
private int size; // 键值对数量 private int size; // 键值对数量
private int capacity = 4; // 哈希表容量 private int capacity = 4; // 哈希表容量
private final double loadThres = 2.0 / 3; // 触发扩容的负载因子阈值 private final double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
private final int extendRatio = 2; // 扩容倍数 private final int extendRatio = 2; // 扩容倍数
private Pair[] buckets; // 桶数组 private Pair[] buckets; // 桶数组
private final Pair TOMBSTONE = new Pair(-1, "-1"); // 删除标记 private final Pair TOMBSTONE = new Pair(-1, "-1"); // 删除标记

@ -24,7 +24,7 @@ class HashMapChaining {
constructor() { constructor() {
this.#size = 0; this.#size = 0;
this.#capacity = 4; this.#capacity = 4;
this.#loadThres = 2 / 3.0; this.#loadThres = 2.0 / 3.0;
this.#extendRatio = 2; this.#extendRatio = 2;
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []); this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
} }

@ -17,7 +17,7 @@ class HashMapChaining:
"""构造方法""" """构造方法"""
self.size = 0 # 键值对数量 self.size = 0 # 键值对数量
self.capacity = 4 # 哈希表容量 self.capacity = 4 # 哈希表容量
self.load_thres = 2 / 3 # 触发扩容的负载因子阈值 self.load_thres = 2.0 / 3.0 # 触发扩容的负载因子阈值
self.extend_ratio = 2 # 扩容倍数 self.extend_ratio = 2 # 扩容倍数
self.buckets = [[] for _ in range(self.capacity)] # 桶数组 self.buckets = [[] for _ in range(self.capacity)] # 桶数组

@ -17,7 +17,7 @@ class HashMapOpenAddressing:
"""构造方法""" """构造方法"""
self.size = 0 # 键值对数量 self.size = 0 # 键值对数量
self.capacity = 4 # 哈希表容量 self.capacity = 4 # 哈希表容量
self.load_thres = 2 / 3 # 触发扩容的负载因子阈值 self.load_thres = 2.0 / 3.0 # 触发扩容的负载因子阈值
self.extend_ratio = 2 # 扩容倍数 self.extend_ratio = 2 # 扩容倍数
self.buckets: list[Pair | None] = [None] * self.capacity # 桶数组 self.buckets: list[Pair | None] = [None] * self.capacity # 桶数组
self.TOMBSTONE = Pair(-1, "-1") # 删除标记 self.TOMBSTONE = Pair(-1, "-1") # 删除标记

@ -18,7 +18,7 @@ class HashMapChaining {
init() { init() {
size = 0 size = 0
capacity = 4 capacity = 4
loadThres = 2 / 3 loadThres = 2.0 / 3.0
extendRatio = 2 extendRatio = 2
buckets = Array(repeating: [], count: capacity) buckets = Array(repeating: [], count: capacity)
} }

@ -19,7 +19,7 @@ class HashMapOpenAddressing {
init() { init() {
size = 0 size = 0
capacity = 4 capacity = 4
loadThres = 2 / 3 loadThres = 2.0 / 3.0
extendRatio = 2 extendRatio = 2
buckets = Array(repeating: nil, count: capacity) buckets = Array(repeating: nil, count: capacity)
removed = Pair(key: -1, val: "-1") removed = Pair(key: -1, val: "-1")

@ -26,7 +26,7 @@ class HashMapChaining {
constructor() { constructor() {
this.#size = 0; this.#size = 0;
this.#capacity = 4; this.#capacity = 4;
this.#loadThres = 2 / 3.0; this.#loadThres = 2.0 / 3.0;
this.#extendRatio = 2; this.#extendRatio = 2;
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []); this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
} }

@ -429,7 +429,7 @@
以上述的求和函数为例,设问题 $f(n) = 1 + 2 + \dots + n$ 。 以上述的求和函数为例,设问题 $f(n) = 1 + 2 + \dots + n$ 。
- **迭代**:在循环中模拟求和过程,从 $1$ 遍历到 $n$ ,每轮执行求和操作,即可求得 $f(n)$ 。 - **迭代**:在循环中模拟求和过程,从 $1$ 遍历到 $n$ ,每轮执行求和操作,即可求得 $f(n)$ 。
- **递归**:将问题分解为子问题 $f(n) = n + f(n-1)$ ,不断(递归地)分解下去,直至基本情况 $f(0) = 0$ 时终止。 - **递归**:将问题分解为子问题 $f(n) = n + f(n-1)$ ,不断(递归地)分解下去,直至基本情况 $f(1) = 1$ 时终止。
### 调用栈 ### 调用栈

@ -29,7 +29,7 @@
以下代码给出了链式地址哈希表的简单实现,需要注意两点。 以下代码给出了链式地址哈希表的简单实现,需要注意两点。
- 使用列表(动态数组)代替链表,从而简化代码。在这种设定下,哈希表(数组)包含多个桶,每个桶都是一个列表。 - 使用列表(动态数组)代替链表,从而简化代码。在这种设定下,哈希表(数组)包含多个桶,每个桶都是一个列表。
- 以下实现包含哈希表扩容方法。当负载因子超过 $0.75$ 时,我们将哈希表扩容至 $2$ 倍。 - 以下实现包含哈希表扩容方法。当负载因子超过 $\frac{2}{3}$ 时,我们将哈希表扩容至 $2$ 倍。
=== "Python" === "Python"

@ -26,7 +26,7 @@
- **数据结构**:基本数据类型,数据结构的分类方法。数组、链表、栈、队列、哈希表、树、堆、图等数据结构的定义、优缺点、常用操作、常见类型、典型应用、实现方法等。 - **数据结构**:基本数据类型,数据结构的分类方法。数组、链表、栈、队列、哈希表、树、堆、图等数据结构的定义、优缺点、常用操作、常见类型、典型应用、实现方法等。
- **算法**:搜索、排序、分治、回溯、动态规划、贪心等算法的定义、优缺点、效率、应用场景、解题步骤、示例题目等。 - **算法**:搜索、排序、分治、回溯、动态规划、贪心等算法的定义、优缺点、效率、应用场景、解题步骤、示例题目等。
![Hello 算法内容结构](about_the_book.assets/hello_algo_mindmap.png) ![Hello 算法内容结构](about_the_book.assets/hello_algo_mindmap.jpg)
## 致谢 ## 致谢

Loading…
Cancel
Save