zoukankan      html  css  js  c++  java
  • 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

     Lintcode上的一道题,没有什么技术含量,就是根据公式求hash值。但是这种straight forward的方法最后超时了,所以其实取余运算可以进行优化,也即是不在最后进行取余运算,而是在每一步的运算中都取余。代码如下:

    class Solution:
        """
        @param key: A String you should hash
        @param HASH_SIZE: An integer
        @return an integer
        """
        def hashCode(self, key, HASH_SIZE):
            if not key:
                return 0
            hashcode = 0
            for s in key:
                hashcode = (hashcode*33 + ord(s))%HASH_SIZE
            return hashcode

    这种操作的正确性可以证明,即 (a+b)%c = (a%c+b)%c  从性质(1)可以推出。

    (a + b) % p = (a % p + b % p) % p (1)
    (a - b) % p = (a % p - b % p) % p (2)
    (a * b) % p = (a % p * b % p) % p (3)
    (a^b) % p = ((a % p)^b) % p (4)
    推论:
    若a≡b (% p),则对于任意的c,都有(a + c) ≡ (b + c) (%p);(10)
    若a≡b (% p),则对于任意的c,都有(a * c) ≡ (b * c) (%p);(11)
    若a≡b (% p),c≡d (% p),则 (a + c) ≡ (b + d) (%p),(a - c) ≡ (b - d) (%p),
    (a * c) ≡ (b * d) (%p),(a / c) ≡ (b / d) (%p); (12)
     
    费马定理:若p是素数,a是正整数且不能被p整除,则:a^(p-1) mod p = 1 mod p
             推论:若p是素数,a是正整数且不能被p整除,则:a^p mod p = a mod p
  • 相关阅读:
    1,300萬像素Xperia TX K.O.相機 東方日報
    信息检索Reading List
    雷军:小米二为何不用1300万像素相机_TechWeb
    1300万像素高清双核旗舰 索尼LT30p评测_手机_科技时代_新浪网
    Darts: DoubleARray Trie System海 的 声音我的搜狐
    说说底层架构之实体类的设计
    不忘本~枚举
    两种底层数据层操作时的架构方式,你喜欢那种?
    说说C#中的global
    JS对外部文件的加载及对IFRMAME的加载的实现,当加载完成后,指定指向方法(方法回调)
  • 原文地址:https://www.cnblogs.com/sherylwang/p/5589223.html
Copyright © 2011-2022 走看看