class Solution:
"""
@param key: A String you should hash
@param HASH_SIZE: An integer
@return an integer
"""
def hashCode(self, key, HASH_SIZE):
# write your code here
re, lenA = 0, len(key)
a = map(ord, key)
for i in range(lenA):
re += a[i] * (33**(lenA - i - 1)) # 幂运算超时
re = re % HASH_SIZE
return re
def hashCode(self, key, HASH_SIZE):
# # write your code here
re, lenA, base = 0, len(key), 1
a = map(ord, key)
for i in range(lenA):
re += a[lenA - i - 1] * base % HASH_SIZE
base = base * 33 % HASH_SIZE
return re % HASH_SIZE
lintcode:哈希函数
最新推荐文章于 2019-05-20 12:43:45 发布