要考试了,每天打卡变少,考完继续加油!
'use strict';
class HashMap {
constructor () {
this.table = []
}
// 散列函数
loseloseHashCode (key) {
let hash = 0;
for(let i = 0; i < key.length; i++) {
hash += key[i].charCodeAt()
}
return hash % 37
}
put (key, value) {
let position = this.loseloseHashCode(key)
console.log(position + '-' + key);
this.table[position] = value
}
remove (key) {
this.table[this.loseloseHashCode(key)] = undefined
return 'ok!'
}
get (key) {
return this.table[this.loseloseHashCode(key)]
}
}
let hashmap = new HashMap();
console.log('TEST1: put()');
hashmap.put('one', 1)
console.log('TEST2: get()');
console.log(hashmap.get('one'));
console.log('TEST3: remove()');
console.log(hashmap.remove('one'));
console.log(hashmap.get('one'));