chapter_hashing/hash_map/ #78
Replies: 87 comments 104 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.
-
简单哈希表C语言实现中, MapSet结构应该如何定义? |
Beta Was this translation helpful? Give feedback.
-
数组添加元素O(1) ? 还是我 有问题 |
Beta Was this translation helpful? Give feedback.
-
您好! 收到————张贤平
|
Beta Was this translation helpful? Give feedback.
-
Java 实现 HashMapArrayList 实现,兼容 String、Integer、Boolean 作为 KEY,一起学习:dsa package top.mphy.algo.basic.core.hashmap;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
public class ArrayListHashMap<K, V> {
private static final int NOT_FOUND = -1;
private static final int MAX_SIZE = 100;
/**
* 键值对节点 接口定义
* @param <K>
* @param <V>
*/
public interface Entry<K, V> {
K getKey();
V getValue();
}
/**
* 键值对节点实现
* @param <K>
* @param <V>
*/
private static class Node<K, V> implements ArrayListHashMap.Entry<K, V> {
private K key;
private V value;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
}
/**
* 桶列表,一个桶存放一个结果
*/
private List<Node<K, V>> buckets;
public ArrayListHashMap() {
buckets = new ArrayList<>(MAX_SIZE);
for (int i = 0; i < MAX_SIZE + 1; i++) {
buckets.add(null);
}
}
/**
* 哈希函数:int
* @param key
* @return
*/
private int hashFunc(Integer key) {
return key % MAX_SIZE;
}
/**
* 哈希函数:String
* @param key
* @return
*/
private int hashFunc(String key) {
int hash = 0;
for (int i = 0; i < key.length(); i++) {
hash = (31 * hash + key.charAt(i)) % MAX_SIZE; // 使用 31 作为乘数,取模 MAX_SIZE
}
return hash;
}
/**
* 哈希函数:Boolean
* @param key
* @return
*/
private int hashFunc(Boolean key) {
if (key == null) {
return MAX_SIZE;
}
if (key) {
return 1;
}
return 0;
}
/**
* 获取 hash 值
* @param key
* @return
*/
private int getHash(K key) {
if (key == null) {
return MAX_SIZE;
}
if (key instanceof Integer) {
return hashFunc((Integer) key);
}
if (key instanceof String) {
return hashFunc((String) key);
}
if (key instanceof Boolean) {
return hashFunc((Boolean) key);
}
return NOT_FOUND;
}
public V get(K key) {
int index = getHash(key);
if (index == NOT_FOUND) return null;
Node<K, V> node = buckets.get(index);
if (node == null) return null;
return node.value;
}
public void put(K key, V value) {
int index = getHash(key);
if (index == NOT_FOUND) return;
buckets.set(index, new Node<>(key, value));
}
public void remove(K key) {
int index = getHash(key);
if (index == NOT_FOUND) return;
buckets.set(index, null);
}
/**
* 获取所有键
* @return
*/
public List<K> keySet() {
List<K> list = new ArrayList<>();
for (Node<K, V> bucket : buckets) {
if (bucket != null) {
list.add(bucket.key);
}
}
return list;
}
/**
* 获取所有值
* @return
*/
public List<V> valueSet() {
List<V> list = new ArrayList<>();
for (Node<K, V> bucket : buckets) {
if (bucket != null) {
list.add(bucket.value);
}
}
return list;
}
/**
* 获取所有键值对
* @return
*/
public List<ArrayListHashMap.Entry<K, V>> entrySet() {
List<ArrayListHashMap.Entry<K, V>> list = new ArrayList<>();
for (ArrayListHashMap.Entry<K, V> bucket : buckets) {
if (bucket != null) {
list.add(bucket);
}
}
return list;
}
/**
* 遍历接口
* @param consumer
*/
public void forEach(BiConsumer<K, V> consumer) {
for (Node<K, V> node : buckets) {
if (node != null) {
consumer.accept(node.key, node.value);
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
day05 |
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.
-
您好! 收到————张贤平
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
您好! 收到————张贤平
|
Beta Was this translation helpful? Give feedback.
-
请教大佬,表6-1与表4-1的部分复杂度是冲突的?个人认为表4-1表述是正确的,也许是表6-1中写错了?? |
Beta Was this translation helpful? Give feedback.
-
哈希斌かわいいね🥹 |
Beta Was this translation helpful? Give feedback.
-
你C语言哈希表里MapSet的实现都没写就直接用?😅 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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