定义:每一个字符都和一个索引相对应;
查找的复杂度O(1);
哈希函数:“键”转换成索引的转换函数;
哈希冲突:两个不同的键对应一个索引;

哈希函数的设计是很重要的;
键通过哈希函数得到的索引的索引越均匀越好
哈希函数的设计:转成整型(并不是唯一的办法)


大整数取模一个素数,让分布更均匀 ;选一个合适的素数参考网站

字符串设计

复合类型转换–和字符串类似
性质:
- 一致性:如果a==b,则hash(a)=hash(b);
- 高效性:计算高校简便;
- 均匀性:哈希值均匀分布;
JAVA中的hashCode
Java中每个Object自带hashCode是根据对象地址生成的,同样的内容不同的对象地址也不一样;
我们可以自定义覆盖hashCode计算方法;
通常要比较两个类是否相等:覆盖hashcode和equals
- hashcode相等证明他们的内容相等
- 同时如果equals相等证明地址也相同,是用一个类
哈希冲突的处理 链地址法 Seperate Chaining


自己做一个哈希表
设计思想:
- 表:顾名思义有行有列
- 设计一个用户自定义大小的数组列
- 每个列中的行设计链表,成TreeMap;(TreeMap的底层实现是红黑树)
定义一个表
add()
Remove()

Set() Contains()

Test

时间复杂度

为实现O(1)级别的复杂度,和静态数组一样,需要reSize
动态空间处理

reSize()

哈希碰撞攻击(了解)
Other:
- leetcode第378题字符串中的第一个唯一的字符