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 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) * 33^3 + ascii(b) * 33^2 + ascii(c) *33 + ascii(d)) % HASH_SIZE
= (97* 33^3 + 98 * 33^2 + 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
Example
For key="abcd" and size=100, return 78
Clarification
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.
对于overflow要特别小心,即便是intermediate result,也要小心overflow
跟Fast Power那道题类似,modular multiplication addition的证明在此
- (a + b) % p = (a % p + b % p) % p (1)
- (a - b) % p = (a % p - b % p) % p (2)
- (a * b) % p = (a % p * b % p) % p (3)
- a ^ b % p = ((a % p)^b) % p (4)
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 result = 0;
int base = 1;
for (int i=key.length-1; i>=0; i--) {
int num = (int)(key[i] - '\0');
result += modMultiply(num, base, HASH_SIZE);
result = result % HASH_SIZE;
base = modMultiply(base, 33, HASH_SIZE);
}
return result % HASH_SIZE;
}
public int modMultiply(int a, int b, int c) {
long temp = (long)a * b;
return (int)(temp % c);
}
};
15行如果如果每次都 base *= 33,即使把base 定义为long型,String一长也会溢出的; 我又试了用Math.pow(33, i), 一样overflow。说明强行算33的power还是不行的,是会溢出的。还是要用Modular Multiplication
(A * B) mod C, 需要注意的是,A、B、C都定义为int,A*B要小心overflow, 所以用long来存这个乘积,然后mod C之后再把结果存成int型
第21行,如果是就是错的:a * b已经overflow了
public int modMultiply(int a, int b, int c) {
long temp = a * b;
return (int)(temp % c);
}
};
处理方法可以是:long temp = (long) a * b; 或者a和b都定义为long型然后long temp = a * b
方法二:不用long type, 网上别人的做法,没看懂
public int hashCode(char[] key,int HASH_SIZE) {
int result = 0;
for (int i = 0; i < key.length; i++) {
result = helper(result, 33, HASH_SIZE);
result += key[i];
result %= HASH_SIZE;
}
return result;
}
int helper(int num, int base, int mod) {
int result = 0;
int temp = num - mod;
for (int i = 0; i < base; i++) {
if (result + temp > 0) {
result += temp;
} else {
result += num;
}
}
return result;
}
Lintcode: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long的更多相关文章
- 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 ...
- POJ1060 Modular multiplication of polynomials解题报告 (2011-12-09 20:27:53)
Modular multiplication of polynomials Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3 ...
- POJ 1060:Modular multiplication of polynomials
Modular multiplication of polynomials Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4 ...
- STL标准库-一个万用的hash function
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 在前面我介绍过hash的使用,本次主要介绍一下Hash Function Hash Function即获得hash code的函 ...
- hash function比较
http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用bo ...
- POJ1060 Modular multiplication of polynomials
题目来源:http://poj.org/problem?id=1060 题目大意: 考虑系数为0和1的多项式.两个多项式的加法可以通过把相应次数项的系数相加而实现.但此处我们用模2加法来计算系数之和. ...
- 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 ...
随机推荐
- LeetCode 48 Rotate Image(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- [转]13 Hours: The Secret Soldiers of Benghazi
转:http://www.imfdb.org/wiki/13_Hours:_The_Secret_Soldiers_of_Benghazi The following weapons were use ...
- shell中的环境变量:local,global,export
1.local一般用于局部变量声明,多在在函数内部使用.实例如下: echo_start() { local STR="$1" echo "...... ${ ...
- tornado 数据库操作
tornado是python的web框架,web程序开发中数据库操作是必须的. 安装: tornado的官方文档中提供的说明比较少,而且提供的模块中未找到数据库方面的模块,难道没有针对数据库操作进行封 ...
- IIS7配置伪静态把后缀名映射为html方案
1.在IIS新建站点.[创建的时候不用去选择版本和模式,默认即可] 2.选中站点,切换到功能试图,找到“处理程序映射",双击之后,在打开窗口右侧的操作栏目下做如下设置: 1)右边" ...
- -bash: locate: command not found
部分版本的linux系统使用locate快速查找某文件路径会报以下错误: -bash: locate: command not found 其原因是没有安装mlocate这个包 安装:yum -y ...
- [EF]vs15+ef6+mysql code first方式
写在前面 前面有篇文章,尝试了db first方式,但不知道是什么原因一直没有成功,到最后也没解决,今天就尝试下code first的方式. [EF]vs15+ef6+mysql这个问题,你遇到过么? ...
- 洛谷P1117 优秀的拆分【Hash】【字符串】【二分】【好难不会】
题目描述 如果一个字符串可以被拆分为AABBAABB的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串aabaabaaaabaabaa,如果令 A=aabA ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- ZOJ 3985 - String of CCPC - [字符串处理]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3985 题意: 给出一个长度为n的字符串,全部由'C'和'P'组成 ...