Lintcode129 Rehashing solution 题解
【题目描述】
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.
·
哈希表容量的大小在一开始是不确定的。如果哈希表存储的元素太多(如超过容量的十分之一),我们应该将哈希表容量扩大一倍,并将所有的哈希值重新安排。假设你有如下一哈希表:
size=3,capacity=4
[null, 21, 14, null]
↓ ↓
9 null
↓
null
哈希函数为:
int hashcode(int key, int capacity) {
return key % capacity;
}
这里有三个数字9,14,21,其中21和9共享同一个位置因为它们有相同的哈希值1(21 % 4 = 9 % 4 = 1)。我们将它们存储在同一个链表中。
重建哈希表,将容量扩大一倍,我们将会得到:
size=3,capacity=8
index: 0 1 2 3 4 5 6 7
hash : [null, 9, null, null, null, 21, 14, null]
给定一个哈希表,返回重哈希后的哈希表。
【注】:哈希表中负整数的下标位置可以通过下列方式计算:
·C++/Java:如果你直接计算-4 % 3,你会得到-1,你可以应用函数:a % b = (a % b + b) % b得到一个非负整数。
·Python:你可以直接用-1 % 3,你可以自动得到2。
·
【题目链接】
[www.lintcode.com/en/problem/rehashing/]()
【题目解析】
此题的难度不大,只需要按照题目的要求实现代码就可以。不过需要注意的是:
1、C++/Java中,不能直接对负数使用取模运算,而需要用等式a % b = (a % b + b) % b,让所得到的hash值为非负数。
2、所得到的新的HashTable中,可能依然存在碰撞,所以仍然需要在对应hashcode位置的ListNode tail上插入新的ListNode。
【参考答案】
[www.jiuzhang.com/solutions/rehashing/]()
转载于:https://blog.51cto.com/13517018/2051491
Lintcode129 Rehashing solution 题解相关推荐
- Lintcode18 Subsets II solution 题解
[题目描述] Given a list of numbers that may has duplicate numbers, return all possible subsets Notice:Ea ...
- Lintcode42 Maximum Subarray II solution 题解
[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The ...
- Lintcode108 Palindrome Partitioning || solution 题解
[题目描述] Given a strings, cutsinto some substrings such that every substring is a palindrome. Return t ...
- Lintcode363 Trapping Rain Water solution 题解
[题目描述] Given n non-negative integers representing an elevation map where the width of each bar is 1, ...
- Lintcode132 Word Search || solution 题解
[题目描述] Given a matrix of lower alphabetsand a dictionary.Find all words in the dictionary that can b ...
- Lintcode107 Word Break solution 题解
[题目描述] Given a string s and a dictionary of words dict, determine if s can be break into a space-sep ...
- Lintcode189 First Missing Positive solution 题解
[题目描述] Given an unsorted integer array, find the first missing positive integer. 给出一个无序的整数数组,找出其中没有出 ...
- Lintcode99 Reorder List solution 题解
[题目描述] Given a singly linked list L: L0→ L1→ - → Ln-1→ Ln reorder it to: L0→ Ln→ L1→ Ln-1→ L2→ Ln-2→ ...
- Lintcode9 Fizz Buzz solution 题解
[题目描述] Given number n. Print number from 1 to n. But: when number is divided by 3, print "fizz& ...
- Lintcode165 Merge Two Sorted Lists solution 题解
[题目描述] Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted ...
最新文章
- 强势推荐8个功能强大,鲜为人知的实用软件
- Apache Hadoop 2.7如何支持读写OSS
- 不支持图形化界面的Linux系统如何显示图像化界面?飞腾服务器显示图像化界面方法,DISPLAY environment variable is undefined问题解决方法
- 【20090319-02】asp.net 操作 word(转)
- Boost:字符串替换的测试程序
- Mvvm Light Toolkit 入门
- MySQL基础_模糊查询—between and in is null <=>(安全等于)等关建字
- Python爬虫 | Selenium爬取当当畅销图书排行
- 架构真经 | 那些年,我们踩过的缓存坑
- 图深度学习 Deep Learning on Graph
- iOS AudioQueue实现音频的录制和播放(附Demo)
- sqlitespy可以打开MySQL吗_SQLite 数据库访问
- clickhouse 离线安装
- 依概率收敛和依分布收敛(附一道例题)
- JwtToken介绍与使用 超详细保姆级教程 内附详细示例代码
- 打开网站被挂马跳转到博彩页面 解决办法
- 脱口秀与ST洲际的“玩笑”故事,掀开了市场隐秘的角落
- Win11右键怎么直接显示所有选项?
- 完整的连接器设计手册_ODUMAC PUSHLOCK,这款连接器如何赢得客户“芳心”?
- 关于计算机对大学生的重要性的论文,计算机基础论文,关于大学计算机基础课程对大学生就业的影响相关参考文献资料-免费论文范文...
热门文章
- Android网络编程之一个Android下菜单系统模块的实现(客户端—添单功能(上部))
- .net 常识 impersonate user 模拟用户
- ASP.NETSpring.NETNHibernate最佳实践(七)——第3章人事子系统(4)人事子系统小结...
- 微软软件开发技术二十年回顾(MFC篇)
- 【算法学习】【图像增强】【Retinex】White Patch Retinex 程序解读
- day2--numpy
- 飓鼎玩笑傲江湖服务器维护,12月24日维护更新公告 - 笑傲江湖12月24日维护更新公告 - 17173笑傲江湖官网合作专区 - 17173.com中国游戏第一门户站...
- 在php定界符中,PHP中的定界符 - ho俊的个人空间 - OSCHINA - 中文开源技术交流社区...
- yii mysql 操作_YII 数据库相关操作
- mysql闩_MySQL锁概述