Rehashing
The size of the hash table is not determinate at the very beginning. If the total size of keys is too large (e.g. size >= capacity / 10), we should double the size of the hash table and rehash every keys. Say you have a hash table looks like below:
size=3
, capacity=4
[null, 21, 14, null]
↓ ↓
9 null
↓
null
The hash function is:
int hashcode(int key, int capacity) {
return key % capacity;
}
here we have three numbers, 9, 14 and 21, where 21 and 9 share the same position as they all have the same hashcode 1 (21 % 4 = 9 % 4 = 1). We store them in the hash table by linked list.
rehashing this hash table, double the capacity, you will get:
size=3
, capacity=8
index: 0 1 2 3 4 5 6 7
hash : [null, 9, null, null, null, 21, 14, null]
Given the original hash table, return the new hash table after rehashing .
Notice
For negative integer in hash table, the position can be calculated as follow:
- C++/Java: if you directly calculate -4 % 3 you will get -1. You can use function: a % b = (a % b + b) % b to make it is a non negative integer.
- Python: you can directly use -1 % 3, you will get 2 automatically.
Given [null, 21->9->null, 14->null, null]
,
return [null, 9->null, null, null, null, 21->null, 14->null, null]
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param hashTable: A list of The first node of linked list
* @return: A list of The first node of linked list which have twice size
*/
public ListNode[] rehashing(ListNode[] hashTable) {
if (hashTable == null || hashTable.length == ) return hashTable; ListNode[] newHT = new ListNode[hashTable.length * ]; for (int i = ; i < hashTable.length; i++) {
ListNode current = hashTable[i];
while (current != null) {
ListNode next = current.next;
current.next = null;
add(newHT, current);
current = next;
}
}
return newHT;
} public void add(ListNode[] hashTable, ListNode node) {
int index = node.val % hashTable.length;
if (index < ) {
index = hashTable.length + index;
}
if (hashTable[index] == null) {
hashTable[index] = node;
} else {
ListNode current = hashTable[index];
while (current.next != null) {
current = current.next;
}
current.next = node;
}
}
};
Rehashing的更多相关文章
- Lintcode: Rehashing
The size of the hash table is not determinate at the very beginning. If the total size of keys is to ...
- A brief introduction to Hashing and Rehashing
偶然发现一篇哈希的综述文章,虽然是1996年写的,里面的一些评测在今天看来早已经小case了.但是今天仍然极具参考价值. 地址:http://www.drdobbs.com/database/hash ...
- 重哈希 · rehashing
[抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: newindex = (hashTable[ ...
- HashMap与TreeMap源码分析
1. 引言 在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- HashMap的工作原理
HashMap的工作原理 HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道HashTable和HashMap之间 ...
- HashMap 源码解析
HashMap简介: HashMap在日常的开发中应用的非常之广泛,它是基于Hash表,实现了Map接口,以键值对(key-value)形式进行数据存储,HashMap在数据结构上使用的是数组+链表. ...
- redis数据结构存储Dict设计细节(redis的设计与实现笔记)
说到redis的Dict(字典),虽说算法上跟市面上一般的Dict实现没有什么区别,但是redis的Dict有2个特殊的地方那就是它的rehash(重新散列)和它的字典节点单向链表. 以下是dict用 ...
- ThreadLocal 源码剖析
ThreadLocal是Java语言提供的用于支持线程局部变量的类.所谓的线程局部变量,就是仅仅只能被本线程访问,不能在线程之间进行共享访问的变量(每个线程一个拷贝).在各个Java web的各种框架 ...
随机推荐
- 5.9-3 用正则表达式判断字符串text是否为合法的手机号
package zfc; public class Zfc { public static void main(String[] args) { //判断手机号格式是否合法 String text = ...
- Google-解决在调试页面或者js时总是提示烦恼的断点问题
按F12键,然后切换到Source标签,看底下的那个跟暂停一样的图标是不是变成蓝色或紫色了? 如果是蓝色或者紫色,则把他切换到“灰色”状态(点击图标就会切换成不同的状态.或者可能是其他颜色状态),如下 ...
- Java设计模式-访问者模式(Visitor)
访问者模式把数据结构和作用于结构上的操作解耦合,使得操作集合可相对自由地演化.访问者模式适用于数据结构相对稳定算法又易变化的系统.因为访问者模式使得算法操作增加变得容易.若系统数据结构对象易于变化,经 ...
- mysql-分页查询方案
一.直接使用limit最简单查询方法: , 在中小数据量的情况下,这样的SQL足够用了,唯一需要注意的问题就是确保使用了索引. 随着数据量的增加,页数会越来越多,查看后几页的SQL就可能类似: , 言 ...
- Learn sed using these command on Linux(流线式编辑器——sed)
是对文件中的每一行进行处理,不会对源文件进行修改 sed --version sed '11d' sed_file sed -n '/[Bb]erry/p' sed_file (由于设置了n,所以只打 ...
- python 学习笔记3(循环方式;list初始化;循环对象/生成器/表推导;函数对象;异常处理)
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 16. 循环方式笔记: 1)range(0, 8, 2) #(上限,下限,步长) 可以实现对元素或者下标的 ...
- Linux Systemcall By INT 0x80、Llinux Kernel Debug Based On Sourcecode
目录 . 系统调用简介 . 系统调用跟踪调试 . 系统调用内核源码分析 1. 系统调用简介 关于系统调用的基本原理,请参阅另一篇文章,本文的主要目标是从内核源代码的角度来学习一下系统调用在底层的内核中 ...
- PHP 数据库驱动、连接数据不同方式学习笔记
相关学习资料 http://www.php.net/manual/zh/refs.database.php http://www.php.net/manual/zh/internals2.pdo.ph ...
- javascript-如何判断一个对象为数组
Q:如何判断一个对象是否为数组? A1:判断对象的constructor是否指向Array, 接着判断对应的特殊属性,如length,splice之类.这个很容易冒充. A2:使用instanceof ...
- 彻底理解position与anchorPoint
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...