Lintcode: 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->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的更多相关文章
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
- Lintcode 372. O(1)时间复杂度删除链表节点
----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...
- Lintcode 469. 等价二叉树
----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...
随机推荐
- 理解OAuth 2.0[摘]
原文地址:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到 ...
- 1 2 3 n
n(n+1)/2 连续自然数 1,2,3.....,n 队列 从中任意取出1至n个相加,可以表示的连续自然数队列中最大的自然数是多少 受"高斯求和--蛇头蛇尾脑图--长方形对角线脑图--苯环 ...
- static的作用,this(),super()用法
1:static{}表示静态代码块:在java虚拟机(jvm)加载该类时,会执行这个代码块一次,静态代码块在new()对象之前就加载了 2: this()与surper()区别:surper()是从子 ...
- Advanced CSharp Messenger
http://wiki.unity3d.com/index.php?title=Advanced_CSharp_Messenger Author: Ilya Suzdalnitski Contents ...
- maven 添加本地jar
方式一 Xml代码 <dependency> <groupId>org.apache</groupId> <artifactId>test</ar ...
- oracle 嵌套表
--自定义对象 CREATE OR REPLACE TYPE Fas_checksheetinfo_line_obj AS OBJECT( CSID_ID VARCHAR2(32 ...
- 用户登录验证例题用的ajax
1.登录页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- 【摘自网络】陈奕迅&&杨千嬅
揭陈奕迅杨千嬅相爱18年恋人未满的点滴片段 文/一床情书 但凡未得到,但凡是过去,总是最登对 ——题记 已经仙逝多年的香港歌坛天后梅艳芳曾经在<似是故人来>里唱道:“但凡未得到,但凡是过去 ...
- 实验一补充内容 Java开发环境的熟悉-刘蔚然
本次实验 PSP时间统计 步骤 耗时百分比 需求分析 5% 设计 10% 代码实现 67% 测试 15% 分析总结 3%
- NGINX原理分析 之 SLAB分配机制
1 引言 众所周知,操作系统使用伙伴系统管理内存,不仅会造成大量的内存碎片,同时处理效率也较低下.SLAB是一种内存管理机制,其拥有较高的处理效率,同时也 有效的避免内存碎片的产生,其核心思想是预分配 ...