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 ...
随机推荐
- Translation Lookaside Buffer
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In principle, then, e ...
- The world beyond batch: Streaming 101
https://www.oreilly.com/ideas/the-world-beyond-batch-streaming-101 https://www.oreilly.com/ideas/the ...
- for循环经典案例
1.棋盘放粮食棋盘有32个格子,第一个格子放1个芝麻,第二个放2个,第三个放4个,第四个放8个...每个芝麻的重量为0.00001kg,如果要放满整个棋盘,需要多少重量的芝麻. <!DOCTYP ...
- 【摘自网络】陈奕迅&&杨千嬅
揭陈奕迅杨千嬅相爱18年恋人未满的点滴片段 文/一床情书 但凡未得到,但凡是过去,总是最登对 ——题记 已经仙逝多年的香港歌坛天后梅艳芳曾经在<似是故人来>里唱道:“但凡未得到,但凡是过去 ...
- 【Android测试】【第四节】LogCat——认识和使用
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4682321.html 什么是LogCat LogCat可以 ...
- 正则表达式lastIndex属性浅析
有这样一段代码: var newDateStr = " 11 13:48:18"; var reg = new RegExp("[0-9]+","g& ...
- A+Bproblem
package A+Bproblem; /* * A+B Problem 时间限制:3000 ms | 内存限制:65535 KB 难度:0 描述 此题为练手用题,请大家计算一下a+b的值 输入 ...
- C++中string转化为常用数值类型
//模板类 用于将string类型转化为 常用数值类型 template <class Type> Type stringToNum(const string& str) { is ...
- 获取网络状态ios(2G、3G、4G、Wifi)
+(NSString *)getNetWorkStates{UIApplication *app = [UIApplication sharedApplication];NSArray *childr ...
- win7:Remote Desktop Services 启动失败
背景: 其他PC使用mstsc远程某win7 pro sp1,一直失败. 分析: 影响远程桌面应用的设置有两个: 1. 计算机远程设置中,启用“允许远程协助连接这台计算机”,且远程桌面设置正确.如选择 ...