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->9->null, 14->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 table: [null, 9, null, null, null, 21, 14, null] Given the original hash table, return the new hash table after rehashing .
Note
For negative integer in hash table, the position can be calculated as follow: In 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. In Python, you can directly use -1 % 3, you will get 2 automatically. Example
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) {
// write your code here
int oldSize = hashTable.length;
int newSize = oldSize * 2;
if (hashTable==null || oldSize==0) return null;
ListNode[] res = new ListNode[newSize];
for (int i=0; i<oldSize; i++) {
if (hashTable[i] != null) rehash(hashTable, res, i);
}
return res;
} public void rehash(ListNode[] hashTable, ListNode[] res, int i) {
int newSize = res.length;
ListNode cur = hashTable[i];
while (cur != null) {
int val = cur.val;
int newPos = val>=0? val%newSize : (val%newSize+newSize)%newSize;
if (res[newPos] == null) res[newPos] = new ListNode(val);
else {
ListNode temp = res[newPos];
while (temp.next != null) {
temp = temp.next;
}
temp.next = new ListNode(val);
}
cur = cur.next;
}
}
};

Lintcode: Rehashing的更多相关文章

  1. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  2. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  3. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  4. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  5. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  6. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  7. Lintcode 175. 翻转二叉树

    -------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...

  8. Lintcode 372. O(1)时间复杂度删除链表节点

    ----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...

  9. Lintcode 469. 等价二叉树

    ----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...

随机推荐

  1. Delphi 指针

    1:指针的赋值. type RTestInfo = record Age:Integer; end; PtestInfo = ^ RtestInfo; var Test1,Test2:PtestInf ...

  2. scandir 使用示例

    int filter_fn(const struct dirent * ent) {     if (ent->d_type != DT_REG)         return 0;     r ...

  3. 低功耗蓝牙4.0BLE编程-nrf51822开发(10)-描述符

    特性中的属性有两种:属性值或描述符. 支持通知或指示的特性中默认有一个描述符:客户端特性配置描述符(Client Characteristic Configuration Descriptor,CCC ...

  4. SAX解析XML

    package ls.xml; import java.io.StreamCorruptedException; import javax.xml.stream.events.EndElement; ...

  5. php--validate错误信息提示样式

    //validate  错误信息提示样式  可以提示错误信息 可以使用jq 自带的属性改变错误的显示的位置,其中element是验证未通过的当前表单元素,error为错误后的提示信息 [注意]:放的位 ...

  6. MongoDB常用操作总结

    ====================================MGDB的操作====================================== 0.创建数据库时使用(use 数据库 ...

  7. new char[]和new char()的区别

    new char[1];分配一块连续的内存,也就是一个数组,里面有1个元素new   char(1);分配一块内存,有一个元素,该元素被初始化为1

  8. MongoDB聚合查询

    1.count:查询记录条数 db.user.count() 它也跟find一样可以有条件的 db.user.count({}) 2.distinct:用来找出给定键的所有不同的值 db.user.d ...

  9. The Simplified Project Management Process

    One of the challenges of explaining project management to people who are unfamiliar with the approac ...

  10. Selenium2学习-019-WebUI自动化实战实例-017-获取浏览器类型

    Web UI 自动化脚本分布执行过程中有时候需要获取浏览器的相关信息,此文给出了一个简略获取浏览器类型的方法,敬请各位小主们参阅.若有不足之处,敬请大神指正,不胜感激! 闲话少述,上码. /** * ...