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

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.

 1 class Solution {
 2     /**
 3      * @param key: A String you should hash
 4      * @param HASH_SIZE: An integer
 5      * @return an integer
 6      */
 7     public int hashCode(char[] key,int HASH_SIZE) {
 8         if (key.length==0) return 0;
 9         int res = 0;
10         int base = 1;
11         for (int i=key.length-1;i>=0;i--){
12             res += modMultiply((int)key[i],base,HASH_SIZE);;
13             res %= HASH_SIZE;
14             base = modMultiply(base,33,HASH_SIZE);
15         }
16         return res;
17     }
18
19     public int modMultiply(long a, long b, int HASH_SIZE){
20         long temp = a*b%HASH_SIZE;
21         return (int) temp;
22     }
23
24 };

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.

 1 class Solution {
 2     /**
 3      * @param key: A String you should hash
 4      * @param HASH_SIZE: An integer
 5      * @return an integer
 6      */
 7     public int hashCode(char[] key,int HASH_SIZE) {
 8         if (key.length==0) return 0;
 9         int res = 0;
10         int base = 33;
11         for (int i=0;i<key.length;i++){
12             res = modMultiply(res,base,HASH_SIZE);
13             res += key[i];
14             res = res % HASH_SIZE;
15         }
16         return res;
17     }
18
19
20     public int modMultiply(int a, int b, int HASH_SIZE){
21         int res = a;
22         for (int j=1;j<b;j++){
23             int temp = (a-HASH_SIZE);
24             if (res+temp>=0)     res = res+temp;
25             else res = res + a;
26         }
27         return res;
28     }
29
30 };
时间: 2024-10-14 21:00:14

LintCode-Hash Function的相关文章

Lintcode: Hash Function &amp;&amp; Summary: Modular Multiplication, Addition, Power &amp;&amp; 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.

Hash function

Hash function From Wikipedia, the free encyclopedia A hash function that maps names to integers from 0 to 15. There is a collision between keys "John Smith" and "Sandra Dee". A hash function is any function that maps data of arbitrary

hash function比较

http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用boost::unordered_map替代前者,发现效率上有很大的提升,但是还是无法达到我们的需求: stl::map  底层算法:B+tree 实现 boost::unordered_map 底层算法:hash 实现 所以可能要针对不同的数据类型编写hash function来优化查找和插入的效率,

General Purpose Hash Function Algorithms

General Purpose Hash Function Algorithms [email protected]: http://www.partow.net/programming/hashfunctions/index.html     Description Hashing Methodologies Hash Functions and Prime Numbers Bit Biases Various Forms Of Hashing String Hashing Cryptogra

lintcode 容易题:Hash Function 哈希函数

题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假设任何字符串都是基于33的一个大整数,比如: hashcode("abcd") = (ascii(a) * 333 + ascii(b) * 332 + ascii(c) *33 + ascii(d)) % HASH_SIZE = (97* 333 + 98 * 332 + 99 * 33

hash function 3种方法 1不好 2一般 3好

1. h(k) =  k mod m its is really bad in the practical. if m = even and k is all even.... ( m is size of hash table, modulo ['m?djul?u ) 2. multiplication method. 好一点 a multiplice the k and sum mod 2^w  w is the bit length integer.  two power of two.

N多校2018d3t10 Hash Function

理解hash过程 首先可知,先放进去肯定是h[i]%n==i的位置,这些个位置放进set按照字典序顺序来,然后每安排一个这样点,对于它后一位的值,如果还没被放入set,则如果它%对应的位置已经放入set了则它放入set ~ ac代码: #include <bits/stdc++.h> using namespace std; #define per(i,a,b) for(int i=a;i<b;i++) #define MP make_pair typedef pair<int,i

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1

Salted hash password

参考文档 http://www.cnblogs.com/richardlee/articles/2511321.html https://en.wikipedia.org/wiki/Salt_%28cryptography%29 https://www.91ri.org/7593.html 密码存储为什么不能是明文? 当账户密码是明文存储的话, 万一本网站给黑客攻破获取了数据, 则用户的账户被泄露.(术语叫 拖库) 当黑客知道了你的账户后, 其可以使用此账户,到其他网站尝试访问, 例如有厉害关系

HASH算法

哈希表(Hash Table)是一种特殊的数据结构,它最大的特点就是可以快速实现查找.插入和删除.因为它独有的特点,Hash表经常被用来解决大数据问题,也因此被广大的程序员所青睐.为了能够更加灵活地使用Hash来提高我们的代码效率,今天,我们就谈一谈Hash的那点事. 回到顶部 1. 哈希表的基本思想 我们知道,数组的最大特点就是:寻址容易,插入和删除困难:而链表正好相反,寻址困难,而插入和删除操作容易.那么如果能够结合两者的优点,做出一种寻址.插入和删除操作同样快速容易的数据结构,那该有多好.