LintCode: Hash Function
生活随笔
收集整理的這篇文章主要介紹了
LintCode: Hash Function
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C++
(1)模運算(百度百科)
(a±b)%p = (a%p±b%p)%p
(a*b)%p = (a%p*b%p)%p
(a^b)%p = ((a%p)^b)%p
(2)使用long型
(3)magic number 33
(4)循環(huán)公式
class Solution { public:/*** @param key: A String you should hash* @param HASH_SIZE: An integer* @return an integer*/int hashCode(string key, int HASH_SIZE) {// write your code hereint len = key.length();int magic = 1;long sum = (int)key[0];for (int i = 1; i < len; i++) {sum = (sum*33)%HASH_SIZE + (int)key[i];}return (int)sum%HASH_SIZE;} };?
本文轉(zhuǎn)自ZH奶酪博客園博客,原文鏈接:http://www.cnblogs.com/CheeseZH/p/5105576.html,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的LintCode: Hash Function的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 脚 本
- 下一篇: Linux基础命令介绍