代码拉取完成,页面将自动刷新
同步操作将从 Gitee 极速下载/javascript-algorithms 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
const DEFAULT_BASE = 17;
export default class SimplePolynomialHash {
/**
* @param {number} [base] - Base number that is used to create the polynomial.
*/
constructor(base = DEFAULT_BASE) {
this.base = base;
}
/**
* Function that creates hash representation of the word.
*
* Time complexity: O(word.length).
*
* @assumption: This version of the function doesn't use modulo operator.
* Thus it may produce number overflows by generating numbers that are
* bigger than Number.MAX_SAFE_INTEGER. This function is mentioned here
* for simplicity and LEARNING reasons.
*
* @param {string} word - String that needs to be hashed.
* @return {number}
*/
hash(word) {
let hash = 0;
for (let charIndex = 0; charIndex < word.length; charIndex += 1) {
hash += word.charCodeAt(charIndex) * (this.base ** charIndex);
}
return hash;
}
/**
* Function that creates hash representation of the word
* based on previous word (shifted by one character left) hash value.
*
* Recalculates the hash representation of a word so that it isn't
* necessary to traverse the whole word again.
*
* Time complexity: O(1).
*
* @assumption: This function doesn't use modulo operator and thus is not safe since
* it may deal with numbers that are bigger than Number.MAX_SAFE_INTEGER. This
* function is mentioned here for simplicity and LEARNING reasons.
*
* @param {number} prevHash
* @param {string} prevWord
* @param {string} newWord
* @return {number}
*/
roll(prevHash, prevWord, newWord) {
let hash = prevHash;
const prevValue = prevWord.charCodeAt(0);
const newValue = newWord.charCodeAt(newWord.length - 1);
hash -= prevValue;
hash /= this.base;
hash += newValue * (this.base ** (newWord.length - 1));
return hash;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。