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. Aliasing 走样

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Have you ever noticed the weird &quo ...

  2. 蓝牙的HFP协议笔记

    1.概述     HFP(Hands-free Profile),可以让蓝牙设备可以控制电话,如接听.挂断.拒接.语音拨号等,拒接.语音拨号要视蓝牙耳机及电话是否支持. HFP定义了音频网关(AG)和 ...

  3. linqPad帮助你快速学习LINQ

    linqPad http://www.cnblogs.com/li-peng/p/3441729.html http://www.linqpad.net/ Linqer http://www.sqlt ...

  4. pomelo架构概览

    pomelo之所以简单易用.功能全面,并且具有高可扩展性.可伸缩性等特点,这与它的技术选型和方案设计是密不可分的.在研究大量游戏引擎设计思路基础上,结合以往游戏开发的经验,确定了pomelo框架的设计 ...

  5. php curl多线程抓取网页

    PHP 利用 Curl Functions 可以完成各种传送文件操作,比如模拟浏览器发送GET,POST请求等等,受限于php语言本身不支持多线程,所以开发爬虫程序效率并不高,这时候往往需 要借助Cu ...

  6. Redis学习笔记(7)-事务

    package cn.com; import java.util.List; import redis.clients.jedis.Jedis; import redis.clients.jedis. ...

  7. SQL Server数据库层面自定义数据同步性能测试

    场景: A DB Server位于上海 B DB Server位于广州 现有特殊需求,需要通过数据链接将数据从A服务器表T1数据同步至B表T2 性能测试: 现模拟T1表9000笔数据 方式一:直接将9 ...

  8. PIVOT&UNPIVOT

    如果是家电销售员,那么可能需要统计每月日销售的彩电.冰箱.空调...最大值.最小值.平均值等 如果你是耳鼻喉科医生,那么可能需要统计月度年度日接客咽炎.喉炎.鼻炎...最大值.最小值.平均值等 如果你 ...

  9. C#访问url地址并返回数据

    public partial class Form1 : Form { static bool isSelect = false; public Form1() { InitializeCompone ...

  10. TCP connection status

    A TCP connection progresses through a series of states during its lifetime. The following diagram il ...