LintCode-Hash Function
In data structure Hash, hash function is used to convert a string(or any other type) into an integer smaller than hash size and bigger or equal to zero. The objective of designing a hash function is to "hash" the key as unreasonable as possible. A good hash function can avoid collision as less as possible. A widely used hash function algorithm is using a magic number 33, consider any string as a 33 based big integer like follow:
hashcode("abcd") = (ascii(a) * 333 + ascii(b) * 332 + ascii(c) *33 + ascii(d)) % HASH_SIZE
= (97* 333 + 98 * 332 + 99 * 33 +100) % HASH_SIZE
= 3595978 % HASH_SIZE
here HASH_SIZE is the capacity of the hash table (you can assume a hash table is like an array with index 0 ~ HASH_SIZE-1).
Given a string as a key and the size of hash table, return the hash value of this key.f
For key="abcd" and size=100, return 78
For this problem, you are not necessary to design your own hash algorithm or consider any collision issue, you just need to implement the algorithm as described.
Analysis:
We need to be careful about overflow, when we calculate the intermiedate result, we need be careful with overflow.
Solution 1:
Use long type.
class Solution {
/**
* @param key: A String you should hash
* @param HASH_SIZE: An integer
* @return an integer
*/
public int hashCode(char[] key,int HASH_SIZE) {
if (key.length==0) return 0;
int res = 0;
int base = 1;
for (int i=key.length-1;i>=0;i--){
res += modMultiply((int)key[i],base,HASH_SIZE);;
res %= HASH_SIZE;
base = modMultiply(base,33,HASH_SIZE);
}
return res;
}
public int modMultiply(long a, long b, int HASH_SIZE){
long temp = a*b%HASH_SIZE;
return (int) temp;
}
};
Solution 2:
Use int type to perform the multiply. However, change the way to calculate the whole expression. Using the way used in solution 1 will cause TLE.
class Solution {
/**
* @param key: A String you should hash
* @param HASH_SIZE: An integer
* @return an integer
*/
public int hashCode(char[] key,int HASH_SIZE) {
if (key.length==0) return 0;
int res = 0;
int base = 33;
for (int i=0;i<key.length;i++){
res = modMultiply(res,base,HASH_SIZE);
res += key[i];
res = res % HASH_SIZE;
}
return res;
}
19
public int modMultiply(int a, int b, int HASH_SIZE){
int res = a;
for (int j=1;j<b;j++){
int temp = (a-HASH_SIZE);
if (res+temp>=0) res = res+temp;
else res = res + a;
}
return res;
}
};
LintCode-Hash Function的更多相关文章
- Lintcode: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long
In data structure Hash, hash function is used to convert a string(or any other type) into an integer ...
- Hash function
Hash function From Wikipedia, the free encyclopedia A hash function that maps names to integers fr ...
- General Purpose Hash Function Algorithms
General Purpose Hash Function Algorithms post@: http://www.partow.net/programming/hashfunctions/inde ...
- STL标准库-一个万用的hash function
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 在前面我介绍过hash的使用,本次主要介绍一下Hash Function Hash Function即获得hash code的函 ...
- hash function比较
http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用bo ...
- You shouldn't use *any* general-purpose hash function for user passwords, not BLAKE2, and not MD5, SHA-1, SHA-256, or SHA-3
hashlib - Secure hashes and message digests - Python 3.8.3 documentation https://docs.python.org/3.8 ...
- 常用加密算法学习总结之散列函数(hash function)
散列函数(Hash function)又称散列算法.哈希函数,散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来.该函数将数据打乱混合,重新创建一个叫做散列值(hash values ...
- 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)
题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...
- hash function 字符串哈希函数
#include <stdio.h> int hash(const char *str) { ; ;;i++) { if (str[i] == '\0') break; sum += (( ...
- STL hash function的模板特化
哈希函数的作用是将一个值映射为一个哈希值,从而根据这个哈希值,在哈希表中对数据进行定位. template <class _Val, class _Key, class _HashFcn, cl ...
随机推荐
- [wordpress] determine_current_user 在get_current_user_id() 或者 wp_get_current_user()会调用
在看了Cookie Authentication In A AngularJS WordPress Theme之后,清楚了当Wordpress在中使用get_current_user_id() 或者w ...
- html+CSS--水平居中设置(定宽块状元素)
来源:http://www.imooc.com/code/4336 当被设置元素为 块状元素 时用 text-align:center 就不起作用了,这时也分两种情况:定宽块状元素和不定宽块状元素. ...
- 一个Itextsharp 批量添加图片到pdf 方法
这里我就直接把我的页面贴进来了 using System; using System.Collections.Generic; using System.Web; using System.Web.U ...
- 【转】PS学堂之一:展示一下自己做的圆形印章
共分七个步骤: 1.点击文件--新建,新建一个500×500像素,背景为透明的文件,选择RGB颜色. 2.把前景色和文字颜色设置为正红(R为255,G和B为0). 3.在视图下拉菜单中选择标尺,将横. ...
- js概念理解
1.面向对象. 2.函数表达式. 3.递归. 4.闭包. 5.原型链. 6.作用域链. 7.上下文. 8.类. 9.活动对象. 10.w3c规范. 11.设计模式. 12.重构. 13.低耦合,高内聚 ...
- MySQL之学生名次问题
--对输入的数据进行约束create table t(studentID char(10), [name] varchar(8), startDate char(10) Check (isdate(s ...
- 入门必须掌握8个DOS命令
一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的:网络上的机器都有唯一确定的IP ...
- Android数据库之SQLite数据库
Android数据库之SQLite数据库 导出查看数据库文件 在android中,为某个应用程序创建的数据库,只有它可以访问,其它应用程序是不能访问的,数据库位于Android设备/data/data ...
- SQL Server 2008R2 禁用远程连接
很多人在开发过程中都会用多数据库(这里仅讨论MSSQL),也都会在服务器上装MSSQL,在你装上MSSQL后,机器上的1433端口就被激活了.如果你的服务器是在内网,也许不用过多的关注,如果你的服务器 ...
- OC9_代理正向传值
// // ProtectedDeleagate.h // OC9_代理正向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) ...