摘要:又用到了取余公式,推導(dǎo)出。用循環(huán)對(duì)數(shù)組各位進(jìn)行轉(zhuǎn)換和相乘和累加。因?yàn)榈诙€(gè)取余公式證明乘積取余與乘數(shù)相加后再取余等價(jià)于乘積取余,所以在每個(gè)循環(huán)內(nèi)都進(jìn)行一次取余,以免乘積太大溢出。
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
ClarificationFor 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.
ExampleFor key="abcd" and size=100, return 78
Note又用到了取余公式: (a * k) % b = [(a % b) * (k % b)] % b,
推導(dǎo)出:(a * b) % b = (a % b + b) % b。
用for循環(huán)對(duì)char數(shù)組各位進(jìn)行hash轉(zhuǎn)換:和33相乘和累加。因?yàn)榈诙€(gè)取余公式證明乘積取余與乘數(shù)相加后再取余等價(jià)于乘積取余,所以在每個(gè)循環(huán)內(nèi)都進(jìn)行一次取余,以免乘積太大溢出。
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; } };
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/65755.html
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...
摘要:求數(shù)組交集不同解法小結(jié)聲明文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處求數(shù)組交集要求元素不重復(fù),給出兩個(gè)數(shù)組,求二者交集且元素不重復(fù),查找會(huì)超時(shí)解法一排序二分查找算法超時(shí)主要發(fā)生在大數(shù)組查找過程,因此采用二分查找提升查找效率,交集用保存實(shí)現(xiàn)去重解法 LintCode547/548_求數(shù)組交集不同解法小結(jié) [TOC] 聲明 文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處:[1] https://segme...
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 ...
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...
摘要:思路原數(shù)組長(zhǎng)度為,則返回原數(shù)組長(zhǎng)度不為,則至少有個(gè)元素。將所有不重復(fù)的數(shù)值賦給,而當(dāng)和相等時(shí),不做處理。最后返回的就是不同元素的個(gè)數(shù),也是新數(shù)組的長(zhǎng)度。只有在時(shí),才對(duì)賦值。注意,每次初始化的時(shí)候要分兩種情況,這就意味著從的時(shí)候開始遍歷。 Remove Duplicates from Sorted Array I Problem Given a sorted array, remove ...
閱讀 1862·2023-04-26 01:58
閱讀 1989·2019-08-30 11:26
閱讀 2733·2019-08-29 12:51
閱讀 3499·2019-08-29 11:11
閱讀 1187·2019-08-26 11:54
閱讀 2102·2019-08-26 11:48
閱讀 3485·2019-08-26 10:23
閱讀 2389·2019-08-23 18:30