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

LintCode-Hash Function的更多相关文章

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

  2. Hash function

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

  3. General Purpose Hash Function Algorithms

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

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

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

  5. hash function比较

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

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

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

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

  8. 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)

    题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...

  9. hash function 字符串哈希函数

    #include <stdio.h> int hash(const char *str) { ; ;;i++) { if (str[i] == '\0') break; sum += (( ...

  10. STL hash function的模板特化

    哈希函数的作用是将一个值映射为一个哈希值,从而根据这个哈希值,在哈希表中对数据进行定位. template <class _Val, class _Key, class _HashFcn, cl ...

随机推荐

  1. Sublime_Tip_01

    //JerryWeb //20150601 //WebTool | Sublime ======= Sublime_Tip_01======= 接触了Sublime后,才真正开始爱上了coding # ...

  2. 【PHP基础】常用mySQL语句以及WampServer2.2设置数据库默认编码

    一.WampServer2.2设置数据库默认编码(此部分转自http://www.cnsecer.com/5984.html) wamp下MySQL的默认编码是Latin1,不支持中文,要支持中文的话 ...

  3. ASP.NET不拖控件教程(1)-认识JSON

    我讲讲脱离ASP.NET控件必备的一步,JSON和使用JQuery获取JSON吧! 高手跳过,写给学习中的人的.这篇帖子是假设你会使用JQuery(JQ这么普及,应该不至少没学过吧!真没学过以后再开帖 ...

  4. 百度网页搜索部来自Console的招聘信息

    百度网页搜索部来自Console的招聘信息,小伙伴们,你发现了吗?

  5. oracle数据库执行脚本常用命令总结

    1. 执行一个SQL脚本文件 代码如下 复制代码 sqlplus user/pass@servicename<file_name.sql或SQL>start file_names或SQL& ...

  6. 分栏控制器和导航栏目tabBarItem,UINavigationController

    ////  AppDelegate.m//  TabBarControllerDemo////  Created by qianfeng on 15/9/22.//  Copyright (c) 20 ...

  7. 20150226—C# winform中的ListView解析

    ListView在WinForm中多用于表的构建,可以直观的显示表的信息,其格式如同SQL的表 这是他的位置,在公共控件中: Listview的几个重要属性:Columms(集合).Groups(集合 ...

  8. Codevs 1009 产生数

    题目描述 Description 给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15). 规则: 一位数可变换成另一个一位数: 规则的右部不能为零. 例如:n=234.有规 ...

  9. LeetCode Weekly Contest 12

    1. 第一题 看完题目后,肯定先对houses和heaters排序,然后考虑贪心可以么,我那时候没有想出来,然后看到可以O(n)的判断一个半径是否满足要求,就对半径[0,1e9]进行二分,然后就a了. ...

  10. Template_16_模板与继承

    1,名称模板参数template <typename PolicySetter1 = DefaultPolicy1,    typename PolicySetter2 = DefaultPol ...