如何在 gethash javascript 函数中查找具有特定字母的字符串
function getHash( string ) {
let h = 7, letters = "acdefhlmnoprstuw";
for( var i = 0; i < string.length; i++ ) {
h = ( h * 37 + letters.indexOf( string[ i ] ) );
}
return h;
}
我的任务是编写一个代码来查找包含以下一些字母的字符串:ACDEFHLMNOPRSTUW
这样函数 getHash(THE SEARCHING STRING) 得到的结果就是 18794359164。
function getHash( string ) {
let h = 7, letters = "acdefhlmnoprstuw";
for( var i = 0; i < string.length; i++ ) {
h = ( h * 37 + letters.indexOf( string[ i ] ) );
}
return h;
}
My task is to write a code which finds the string containing some of following letters: ACDEFHLMNOPRSTUW
Sothat the function getHash(THE SEARCHING STRING) gets the result of 18794359164.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会给你一些代码,只是一些提示。
为了理解代码,你已经并且想要得到的是,用你的命令获取一个字符串并从中获取一个哈希值。
例如,采用
'wonder'
并获取哈希值。
该值包含起始值 7 (
h = 7
),并且对于每个字母,它将h
与37
相乘(该值看起来像是为 37 个不同的字符制作)并添加字母
的索引值。要获得与数值相反的值,您需要用
37
分隔除法的其余部分以获取字母的索引,与使用字母完全相反,并向散列添加一个值。例如,取上面的值
19017519751
并除以37
得到剩下的部分现在用最后一个字母(记住编码从单词的开头到结尾,解码从结尾),您需要获得一个不带最后一个字母的值。
通过编码,您将每个最后的总和乘以 37,这在这里也适用,但以相反的方式,您需要一个整数值。只需除以
37
并获取下一个字母的下限值即可。其余的看起来像这样:
最后,您需要进行检查以停止迭代,并且该值是
7
编码的第一个值。I am not giving you some code, just some hints.
To get an understanding of the code, you have, and the one, you want to get, is to take a string with you gunction and get a hash value from it.
For example take
'wonder'
and getas hash value.
This value contains a start value of seven (
h = 7
) and for each letter it multipliesh
with37
(this value looks like it is made for 37 different characters) and adds the index value ofletters
.To get the opposite with a numerical value, you need to separate a rest of a division by
37
to get an index of a letter, exact the opposite of using a letter and add a value to the hash.For example take the above value
19017519751
and get the rest of the division by37
Now with the last letter (remember encoding goes from start to end of the word, decoding starts with the end), you need get a value without the last letter.
By encoding, you multiply each last sum by 37 and this works here as well, but in reversed manner and you need an integer value. Just divide by
37
and take the floored value for the next letter.The rest looks like this:
Finially, you need to have a check to stop the iteration ans this value is
7
the first value of encoding.