资讯专栏INFORMATION COLUMN

[LintCode] Hash Function

Dogee / 2480人阅读

摘要:又用到了取余公式,推导出。用循环对数组各位进行转换和相乘和累加。因为第二个取余公式证明乘积取余与乘数相加后再取余等价于乘积取余,所以在每个循环内都进行一次取余,以免乘积太大溢出。

Problem

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

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.

Example

For key="abcd" and size=100, return 78

Note

又用到了取余公式: (a * k) % b = [(a % b) * (k % b)] % b
推导出:(a * b) % b = (a % b + b) % b
用for循环对char数组各位进行hash转换:和33相乘和累加。因为第二个取余公式证明乘积取余与乘数相加后再取余等价于乘积取余,所以在每个循环内都进行一次取余,以免乘积太大溢出。

Solution
class Solution {
    public int hashCode(char[] key,int HASH_SIZE) {
        if (key == null) return 0;
        if (HASH_SIZE == 0) return 0;
        long res = 0;
        for (int i = 0; i < key.length; i++) {
            res = res * 33 + key[i];
            res %= HASH_SIZE;
        }
        //res = (res % HASH_SIZE + HASH_SIZE) % HASH_SIZE;
        return (int)res;
    }
};

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65755.html

相关文章

  • [LintCode] Rehashing

    Problem The size of the hash table is not determinate at the very beginning. If the total size of keys is too large (e.g. size >= capacity / 10), we should double the size of the hash table and rehash...

    Jason 评论0 收藏0
  • LintCode547/548_求数组交集不同解法小结

    摘要:求数组交集不同解法小结声明文章均为本人技术笔记,转载请注明出处求数组交集要求元素不重复,给出两个数组,求二者交集且元素不重复,查找会超时解法一排序二分查找算法超时主要发生在大数组查找过程,因此采用二分查找提升查找效率,交集用保存实现去重解法 LintCode547/548_求数组交集不同解法小结 [TOC] 声明 文章均为本人技术笔记,转载请注明出处:[1] https://segme...

    gxyz 评论0 收藏0
  • [LintCode] Nuts & Bolts Problem

    Problem Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Comparison of a nut to another nut or a bolt to another bolt is not ...

    tuomao 评论0 收藏0
  • [LintCode] Move Zeroes

    Problem Given an array nums, write a function to move all 0s to the end of it while maintaining the relative order of the non-zero elements. Notice You must do this in-place without making a copy of t...

    Mr_houzi 评论0 收藏0
  • [LintCode/LeetCode] Remove Duplicates from Sorted

    摘要:思路原数组长度为,则返回原数组长度不为,则至少有个元素。将所有不重复的数值赋给,而当和相等时,不做处理。最后返回的就是不同元素的个数,也是新数组的长度。只有在时,才对赋值。注意,每次初始化的时候要分两种情况,这就意味着从的时候开始遍历。 Remove Duplicates from Sorted Array I Problem Given a sorted array, remove ...

    WalkerXu 评论0 收藏0

发表评论

0条评论

Dogee

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<