chapter_hashing/hash_algorithm/ #556
Replies: 31 comments 37 replies
-
Mark |
Beta Was this translation helpful? Give feedback.
-
看不明白,如果整数的哈希值就是本身的话,不就没有映射了吗?求解答 orz |
Beta Was this translation helpful? Give feedback.
-
大佬们这个上一页和下一页能把不能挪到评论区上面,或者是映射个快捷键,总是去点目录感觉有些不舒服 |
Beta Was this translation helpful? Give feedback.
-
也就是说完备的哈希算法可以广泛运用于数据安全验证中,防篡改 |
Beta Was this translation helpful? Give feedback.
-
现在政府项目开始使用SM3了。 |
Beta Was this translation helpful? Give feedback.
-
为什么我用c++代码跑出来的hash值和文章中的不一致? |
Beta Was this translation helpful? Give feedback.
-
hash算法其中一个重要原则是“确定性”,与上面“随机盐”就矛盾了 |
Beta Was this translation helpful? Give feedback.
-
大佬可以推荐几个相应的练习题网站或者leecode题库吗 |
Beta Was this translation helpful? Give feedback.
-
讲得很透彻,对于入门级来说,是一个不可多得学习资料,谢谢大佬 |
Beta Was this translation helpful? Give feedback.
-
关于哈希函数,推荐一篇文章,可以配合着看 |
Beta Was this translation helpful? Give feedback.
-
书中说加法哈希无法区分内容相同但顺序不同的字符串,是不是内容不同加法哈希也可能产生哈希冲突,比如“ad"和"cb","cam"和"nab" |
Beta Was this translation helpful? Give feedback.
-
请问大佬,旋转哈希中的这个旋转 /* 旋转哈希 */
function rotHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;
}
return hash;
} |
Beta Was this translation helpful? Give feedback.
-
如果说数值的hash值是本身,那么意味着存在某个字符串和某个数值的key, 映射冲突吗? |
Beta Was this translation helpful? Give feedback.
-
这一节的哈希算法是对上两节的value做key吧。也就是对要存的字符串赋予编号 |
Beta Was this translation helpful? Give feedback.
-
数据结构的哈希值中,是不是将数据看成value,其哈希值看成是key? |
Beta Was this translation helpful? Give feedback.
-
hash & mod ,mod不应该是二进制数吗?感谢 |
Beta Was this translation helpful? Give feedback.
-
python里面 hash(-1)=-2,hash(-2)=-2,这是咋回事啊 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
数据完整性检查:数据发送方可以计算数据的哈希值并将其一同发送;接收方可以重新计算接收到的数据的哈希值,并与接收到的哈希值进行比较。如果两者匹配,那么数据就被视为完整。 |
Beta Was this translation helpful? Give feedback.
-
作者您好,在C++中,std::vector的底层实现为迭代器。也就是说动态数组中的元素并不是其成员。那么一个std::vector对象是否为可哈希的呢?因为其成员的内存地址不变,只是其存放元素的地址在变化(即成员迭代器的值变化了) |
Beta Was this translation helpful? Give feedback.
-
作者你好 |
Beta Was this translation helpful? Give feedback.
-
感觉缺少一致性 hash 的介绍。包括 hashring 环法,或者 jumphash 这些。这些在负载均衡领域很通用。 ![]() |
Beta Was this translation helpful? Give feedback.
-
哈希表章节看完了,但还是没有明白这个哈希表到底起什么用,容量如何设计的。如果哈希表是一个数组存储,那么这个章节中讲的hash算法计算key,这个key怎么确保是数组长度内的索引呢?如果哈希表不是数组存储,那么上一章节讲到的线性探测、平方探测等如何能实现呢 |
Beta Was this translation helpful? Give feedback.
-
实在是无法理解哈希表的作用 |
Beta Was this translation helpful? Give feedback.
-
什么情况下,需要将大的输入空间映射到小的输出空间呢?为什么哈希表采用数组结构,而不是直降将key作为字符串索引存储到对象中呢 |
Beta Was this translation helpful? Give feedback.
-
我有一个问题,MD5、SHA-1、SHA-2 和 SHA-3 这些Hash 摘要算法输出的不是一个整数,而编程语言使用的 Hash 算法为什么计算出来是整数?编程语言的 Hash 算法不是使用这些已有的吗? |
Beta Was this translation helpful? Give feedback.
-
#ifndef __CUSTOMHASHMAP_HPP__
#define __CUSTOMHASHMAP_HPP__
/**
* @file customHashMap.hpp
* @brief 基于vector数组实现哈希表,哈希冲突解决方式:链式地址
*/
#include <vector>
#include <list>
#include <tuple>
#include <functional>
template<typename Key, typename Value>
class HashChainContainer;
template<typename Key, typename Value, template<typename, typename> typename Container = HashChainContainer>
class CustomHashMap
{
using Base = CustomHashMap;
public:
const Value& get(const Key& key) const {return m_elements.get(key);}
Value& get(const Key& key){return const_cast<Value&>(const_cast<const Base*>(this)->get(key));}
void put(const Key& key, const Value& value){m_elements.put(key, value);}
void remove(const Key& key){m_elements.remove(key);}
bool empty() const {m_elements.empty();}
Value& operator[](const Key& key){return m_elements[key];}
private:
Container<Key, Value> m_elements;
};
template<typename Key, typename Value>
class HashChainContainer
{
public:
HashChainContainer(){m_elements.resize(m_capacity);};
void put(const Key& key, const Value& value)
{
if ((m_size / (double)m_capacity) >= m_load_factory)
expand();
auto& list = m_elements[std::hash<Key>{}(key) % m_capacity];
auto finder = std::find_if(std::begin(list), std::end(list), [&](auto& element)
{
auto& [e_key, e_value] = element;
return e_key == key;
});
if (finder != std::end(list))
std::get<1>(*finder) = value;
else
{
list.emplace_back(key, value);
++m_size;
}
}
const Value& get(const Key& key) const
{
auto& list = m_elements[std::hash<Key>{}(key) % m_capacity];
auto finder = std::find_if(std::begin(list), std::end(list), [&](auto& element)
{
auto& [e_key, e_value] = element;
return e_key == key;
});
if (finder == std::end(list))
throw std::out_of_range("key not found");
return std::get<1>(*finder);
}
void remove(const Key& key)
{
auto& list = m_elements[std::hash<Key>(key) % m_capacity];
auto finder = std::find_if(std::begin(list), std::end(list), [&](auto& element)
{
auto& [e_key, e_value] = element;
return e_key == key;
});
if (finder != std::end(list))
{
list.erase(finder);
--m_size;
}
}
void expand()
{
m_capacity *= m_expand;
m_elements.resize(m_capacity);
}
bool empty() {return m_size == 0;}
Value& operator[](const Key& key)
{
auto& list = m_elements[std::hash<Key>{}(key) % m_capacity];
auto finder = std::find_if(std::begin(list), std::end(list), [&](auto& element)
{
auto& [e_key, e_value] = element;
return e_key == key;
});
if (finder == std::end(list))
return std::get<1>(list.emplace_back(key, Value{}));
return std::get<1>(*finder);
}
private:
std::size_t m_size{0};
std::size_t m_expand{2};
std::size_t m_capacity{1007};
double m_load_factory{0.75};
std::vector<std::list<std::tuple<Key, Value>>> m_elements;
};
#endif //__CUSTOMHASHMAP_HPP__ |
Beta Was this translation helpful? Give feedback.
-
day05 |
Beta Was this translation helpful? Give feedback.
-
打卡第二天前面身体不舒服,生病漏了几天 |
Beta Was this translation helpful? Give feedback.
-
请问:6.3.1均匀分布 均匀分布会降低冲突后的查询的时间复杂度,但是为什么会降低冲突概率,冲突概率应该是与输入有关吧?求解🤔 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
chapter_hashing/hash_algorithm/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_hashing/hash_algorithm/
Beta Was this translation helpful? Give feedback.
All reactions