LintCode-Hash Function
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
For key="abcd" and size=100, return 78
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的更多相关文章
- 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 ...
- 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 ...
- STL标准库-一个万用的hash function
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 在前面我介绍过hash的使用,本次主要介绍一下Hash Function Hash Function即获得hash code的函 ...
- hash function比较
http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用bo ...
- 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 ...
- 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)
题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...
- hash function 字符串哈希函数
#include <stdio.h> int hash(const char *str) { ; ;;i++) { if (str[i] == '\0') break; sum += (( ...
- STL hash function的模板特化
哈希函数的作用是将一个值映射为一个哈希值,从而根据这个哈希值,在哈希表中对数据进行定位. template <class _Val, class _Key, class _HashFcn, cl ...
随机推荐
- a标签中使用img后的高度多了4px
前两天,在做一个网站的时候,发现a标签中使用img后的高度多了4px,各种纠结. 最后,仔细分析,终于找到原因了,因为img是行内元素,默认display: inline; 它与文本的默认行为类似,下 ...
- DWZ (JUI) 教程 DWZ中dialog层的刷新
在DWZ开发过程中经常会遇到的一种情况就是:在navTab页面中通过a标签打开一个dialog,在dialog层进行操作后,需要对该dialog层进行必要的刷新操作. 1.首先讲一下思路: 在非dia ...
- Linq连接数据库时,sql中的列为自增长,如何添加数据?
1`首先在数据库中写入命令 SET IDENTITY_INSERT 表名 on 2·重新把表拖入 .dbml 中
- 20141030--SQL2008常用命令-1
create database biao2--创建新的数据库 go use biao2 go create table shuiguo--创建表shuiguo ,create table创建表 ( 序 ...
- BeanFactory容器的设计原理
XmlBeanFactory设计的类继承关系 1.BeanFactory接口提供了使用IoC容器的规范.在这个基础上,Spring还提供了符合这个IoC容器接口的一系列容器的实现供开发人员使用. 2. ...
- (转)mongoDB 禁用大内存页面 transparent_hugepage=never
最近在学mongoDB,安装倒没什么困难,有yum仓库.不过接入ctl后的一条warning倒挺让人烦心的. 1 2 2015-03-22T09:27:00.222+0800 I CONTROL [ ...
- javascript弹窗基础篇
confirm()意既确认框 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 8款功能强大的最新HTML5特效实例
1.HTML5 Canvas画板画图工具 可定义笔刷和画布 今天要分享的这款HTML5 Canvas画图工具就可以简单实现网络画图的功能,我们可以自定义笔刷的类型.粗细.颜色,也可以定义画布的大小和背 ...
- java与C#用protobuf通信--java如何转换protobuf-net中的bcl.Decimal对象
公司内部有些C#服务使用proto-net,引入了bcl.proto中的bcl.Decimal.bcl.DateTime等.对于java的proto生成代码需要对bcl.Decimal.bcl.Dat ...
- A@2a139a55 结果产生的原因
程序代码: public class ExplorationJDKSource { /** * @param args */ public static void main(String[] args ...