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 ...
随机推荐
- Android 1.6 PackageManagerService源码分析
文件清单 framework\base\services\core\java\com\android\server\pm\PackageManagerService.java PackageManag ...
- 异构GoldenGate 12c 双向复制配置
1.配置window,添加checkpoint表(本文windows和linux互为source和target) GGSCI (WIN-GM5PVS1CILH) 1> view param ./ ...
- SSH安装篇之——SecureCRT连接(内网和外网)虚拟机中的Linux系统(Ubuntu)
最近在学习Linux,看了网上很多SecureCRT连接本地虚拟机当中的Linux系统,很多都是需要设置Linux的配置文件,有点繁琐,所以自己就摸索了一下,把相关操作贴出来分享一下. SecureC ...
- linux指定某非root用户执行开机启动项的方法(gogs git)
以linux指定git用户在linux开机时执行启动gogs git为例: 以root登录linux 执行vi /etc/rc.d/rc.local 在文档末尾添加一行语句:su git -c &qu ...
- linux下php的一些问题
一.Loaded Configuration File none Configuration File (php.ini) Path /data/service/php/etc 在etc目录下有ph ...
- Python虚拟开发环境pipenv
简介 requests库的作者,requests库的作者,requests库的作者(重要的事情说三遍)的又一力作,囊落了virtualenv, pip, pipfile等作为虚拟开发环境的命令行工具 ...
- 23种设计模式之单例模式(Singleton)
单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类,它提供全局访问的方法. public class SingleTon { private static Si ...
- C++ 标准输出cout与printf
C++标准输出cout与printf都可以,printf用法更死板一些. #include <iostream> int main(int argc, char** argv) { usi ...
- vsftpd java程序无法创建和切换目录
# Example config file /etc/vsftpd/vsftpd.conf # # The default compiled in settings are fairly parano ...
- React 组件协同关系
组件协同的两种方法,1种是纵向的协同,就是组件嵌套,重点在于代码的封装,2种是横向协同也就是Mixin,组件抽离,重点在于代码复用 1.组件嵌套,父组件通过属性向子组件,子组件可以通过事件处理函数以委 ...