LintCode-Hash Function

时间:2021-07-01 09:53:34

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

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.

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;
} };