【发布时间】:2017-12-12 09:14:48
【问题描述】:
假设我的哈希表的桶数是 100。对于密钥 A,我的哈希码为 500,对于密钥 B,我的哈希码为 600。对于 hashCode % this.bucketCount,它们都解析为 0,这是不同哈希码的冲突。
我想知道为什么使用% 来计算索引。有人可以解释一下这个数学吗?为什么该数学输出的索引是我应该插入节点的位置?
HashTable.prototype.hashFunction = function(key){
var hash = 0;
for (var i=0;i< key.length; i++){
console.log(key.charCodeAt(i))
hash += key.charCodeAt(i)
}
return hash;
};
HashTable.prototype.convertHashToIndex = function(hashCode) {
return hashCode % this.bucketCount;
};
【问题讨论】:
标签: javascript hashtable