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的更多相关文章

  1. Hash function

    Hash function From Wikipedia, the free encyclopedia   A hash function that maps names to integers fr ...

  2. General Purpose Hash Function Algorithms

    General Purpose Hash Function Algorithms post@: http://www.partow.net/programming/hashfunctions/inde ...

  3. POJ1060 Modular multiplication of polynomials解题报告 (2011-12-09 20:27:53)

    Modular multiplication of polynomials Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3 ...

  4. POJ 1060:Modular multiplication of polynomials

    Modular multiplication of polynomials Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4 ...

  5. STL标准库-一个万用的hash function

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 在前面我介绍过hash的使用,本次主要介绍一下Hash Function Hash Function即获得hash code的函 ...

  6. hash function比较

    http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用bo ...

  7. POJ1060 Modular multiplication of polynomials

    题目来源:http://poj.org/problem?id=1060 题目大意: 考虑系数为0和1的多项式.两个多项式的加法可以通过把相应次数项的系数相加而实现.但此处我们用模2加法来计算系数之和. ...

  8. 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 ...

  9. 常用加密算法学习总结之散列函数(hash function)

    散列函数(Hash function)又称散列算法.哈希函数,散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来.该函数将数据打乱混合,重新创建一个叫做散列值(hash values ...

随机推荐

  1. ubuntu 14.04 LTS 右键菜单解压压缩包时出错

    先卸载rar sudo apt-get remove rar 再安装unrar sudo apt-get install unrar

  2. JVM工具jcmd实践

    暂时参考以下链接,后续补充自己的实践. https://www.jianshu.com/p/388e35d8a09b

  3. testmath

    $\Huge ans = \frac{ \sum\limits_{i=1}^{m}{ (x_i - \bar{x})^2 } }{m} \cdot m^2 \\$

  4. DragonBones龙骨插槽的隐藏

    参考: 插槽中对象的显示与隐藏问题 let factory = dragonBones.EgretFactory.factory; factory.parseDragonBonesData(RES.g ...

  5. 【CF884F】Anti-Palindromize 费用流

    [CF884F]Anti-Palindromize 题意:定义一个串是反回文的,当且仅当对于1<=i<=len,$a_i!=a_{len-i+1}$. 现在给出一个长度为n的串S(n是偶数 ...

  6. java使用AES256解密

    网上关于java用AES加密解密的文章有很多,我这里只用到解密(加密是服务器那边做^_^),所以更简洁一些: public class AES256Utils { private static fin ...

  7. thinkCMF----列表页跳转

    thinkCMF列表循环有个:用来循环文章列表. <php> $where=[ 'post.create_time'=>['egt',0] ]; $page=[ 'list_rows ...

  8. 极大既然估计和高斯分布推导最小二乘、LASSO、Ridge回归

    最小二乘法可以从Cost/Loss function角度去想,这是统计(机器)学习里面一个重要概念,一般建立模型就是让loss function最小,而最小二乘法可以认为是 loss function ...

  9. oracle11gR2 win7_32位客户端连接虚拟机中oracle11gR2 win7_32位服务器方法

    改写服务器中的监听文件(listener.ora和tnsnames.ora) “ora-12541:TNS:无监听程序”问题的解决 ora-12541:TNS:无监听程序,出现这种错误的时候,可以尝试 ...

  10. 从url到请求 再到页面生成

    百度面试 从url到请求 再到页面生成 - MartinDing - 博客园 https://www.cnblogs.com/martinding/p/7458723.html