chapter_hashing/hash_map/ #78
Replies: 73 comments 92 replies
-
大佬,在”哈希表优势“那一小节中,在“有序数组: 将 1. 中的数组按照学号从小到大排序”中,“1.”找不到对照。4种数据结构用有序列表列出会不会更好一些? |
Beta Was this translation helpful? Give feedback.
-
文章中对比五种数据结构各操作的时间复杂度中,链表的插入元素应该是O(n)。 |
Beta Was this translation helpful? Give feedback.
-
数据结构操作时间复杂度那,链表删除不是O(1)吗? |
Beta Was this translation helpful? Give feedback.
-
在初始化哈希表中(JavaScript 代码): |
Beta Was this translation helpful? Give feedback.
-
这一块儿 typescript 代码是不是不对?获取所有键值对,看起来只 push 了键? |
Beta Was this translation helpful? Give feedback.
-
哈希表底层实现是数组、链表、二叉树,那为什么效率可以更高呢 |
Beta Was this translation helpful? Give feedback.
-
问一下,为什么不使哈希函数f(x) = x 呢。这样就不会有冲突了。 |
Beta Was this translation helpful? Give feedback.
-
受教了,才知道哈希表算的是key和数组下标的关系,之前居然还以为是key 和 value的关系,实在是一点不懂啊哈哈 |
Beta Was this translation helpful? Give feedback.
-
array_hash_map.java
|
Beta Was this translation helpful? Give feedback.
-
哈希表是一个神奇的数据结构。但其实需要细较下它的复杂度,我们仅拿查找 key 为例,人人都说是常数时间复杂度,实际上是过于笼统了,因为没有考虑求 key 的哈希值的复杂度。 比如对于key类型为 string 的哈希表,要在其中找到一个 key,首先需要对这个key做一次哈希计算,字符串的话至少要遍历一次这个字符串,也就是哈希计算的复杂度为 O(m),m 指key的长度,总的查询复杂度也是 O(m)。 当然,对于 int 类型的key,哈希计算的复杂度确实是常数级。 综上,如果 key 是 string 或复合类型(元组等)且 key 的平均长度很长,用哈希表效率非常低。 |
Beta Was this translation helpful? Give feedback.
-
有个疑惑,hash表 时间复杂度为啥是O(1),不应该是O(n) 吗? |
Beta Was this translation helpful? Give feedback.
-
上面数组,链表和哈希表的对比表看蒙了,应该明确一点。数组如果根据下标查询应该是O(1),而插入元素平均复杂度也应该是O(n),怎么就成了O(1)。链表的插入元素的复杂度为啥是O(1),不是应该是O(N)吗? |
Beta Was this translation helpful? Give feedback.
-
我感觉哈希冲突示例图那里容易引起误会。哈希冲突是在添加新元素时发生的,新的元素可能顶掉旧元素,而不是查找时发生的。图里面呈现的是查找过程。 |
Beta Was this translation helpful? Give feedback.
-
打卡,手敲了一遍哈希表的数组实现,对index和key才真正分清楚 |
Beta Was this translation helpful? Give feedback.
-
也就是理解为哈希表比数组链表拥有更小的时间复杂度是用更大的空间复杂度换的吗? |
Beta Was this translation helpful? Give feedback.
-
哈希表的元素数量不是等于桶数量么,求高手解答一下 |
Beta Was this translation helpful? Give feedback.
-
~ArrayHashMap() { |
Beta Was this translation helpful? Give feedback.
-
发现错误:表 6-1 元素查询效率对比 -> 链表删除 |
Beta Was this translation helpful? Give feedback.
-
C++ 字典也可以通花括号来初始化,如: unordered_map<int, string> m = { {1, "one"}, {2, "two"}, {3, "three"} }; |
Beta Was this translation helpful? Give feedback.
-
C++ 的
使用auto 的优点
|
Beta Was this translation helpful? Give feedback.
-
Python 中对于键值的遍历也可用列表解析式进行优化,如: def entry_set(self) -> list[Pair]:
return [pair for pair in self.buckets if pair] |
Beta Was this translation helpful? Give feedback.
-
C 的 void remove(HashMap* map, int key) {
int i = hashFunc(key);
if (!map->buckets[i])
return;
free(map->buckets[i]->val);
free(map->buckets[i]);
map->buckets[i] = NULL;
} |
Beta Was this translation helpful? Give feedback.
-
C#的Pair类是不是错了,?应该是运用于结构体的,对于类来说有无?都是一样的,而且类可以直接写构造函数吗? |
Beta Was this translation helpful? Give feedback.
-
表 6-1 元素查询效率对比,关于数组和链表的访问、插入和删除的时间复杂度是不是弄错了,数组的访问应该时O(1),插入O(n),删除O(n),链表的访问O(n),插入O(1),删除O(1)。 |
Beta Was this translation helpful? Give feedback.
-
~ArrayHashMap() { |
Beta Was this translation helpful? Give feedback.
-
我做了一个哈希表的可视化页面,采用网格布局模拟哈希表的内部结构。 每个格子代表一个哈希桶,格子的颜色深浅直观地反映了该桶中存储的元素数量,颜色越深表示包含的元素越多。可以点击任意格子,系统会显示一个弹窗,展示该桶中存储的所有键值对详细信息。 在进行插入、查找、删除等操作时,系统会通过高亮效果显示当前操作涉及的桶,帮助理解哈希表的工作原理。 |
Beta Was this translation helpful? Give feedback.
-
出现冲突了,那岂不是把之前的数据覆盖了,需要做其他处理的吧 |
Beta Was this translation helpful? Give feedback.
-
图片是用什么软件制作的?求 |
Beta Was this translation helpful? Give feedback.
-
chapter_hashing/hash_map/
Your first book to learn Data Structure And Algorithm.
https://www.hello-algo.com/chapter_hashing/hash_map/
Beta Was this translation helpful? Give feedback.
All reactions