lintcode-easy-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.

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.

这道题要注意的就是溢出,要注意两点:

1. 使用long类型

2. 每次计算33的n次方要再做%HASH_SIZE,每一项累加之后也要%HASH_SIZE避免溢出

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) {
        // write your code here
        if(key == null || key.length == 0)
            return 0;

        long factor = 1;
        long result = 0;

        for(int i = key.length - 1; i >= 0; i--){
            result += (((int) key[i]) * factor) % HASH_SIZE;
            factor = (factor * 33) % HASH_SIZE;

        }

        result %= HASH_SIZE;

        return (int) result;
    }
};
时间: 2024-08-24 17:44:11

lintcode-easy-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 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

[lintcode easy]Merge Sorted Array II

Merge Sorted Array II Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How can you optimize your algorithm if one array is very large and the other is ver

[Lintcode easy]Longest Words

Longest Words Given a dictionary, find all of the longest words in the dictionary. Example Given { "dog", "google", "facebook", "internationalization", "blabla" } the longest words are(is) ["internati

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 easy Happy Number

Happy Number Write an algorithm to determine if a number is happy. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process unt