我怎么会写这样的代码,而所有的重复删除重复的代码
// I have a loop which decrements m each time
// set m to the starting point
m = mid
// set f to a calculated array value
f = dict[l].substr(l * --m, l)
while (f.substr(0,x) === word && (!limit || matches.length < limit)){
matches.push(f);
// same as what was defined outside the while loop
// which seems to me like unnecessary repetition
f = dict[l].substr(l * --m, l)
}
// then I repeat it all, but incrementing m
// reset m to the starting point
m = mid
f = dict[l].substr(l * m++, l)
while (f.substr(0,x) === word && (!limit || matches.length < limit)){
matches.push(f);
f = dict[l].substr(l * m++, l)
}
有两个部分...
- 每个块包含重复
f = ...
部分 - 块被重复,只改变增量/减量
m
编辑:代码做什么...
mid
表示任意入口点的无隔板固定长度的单词的字母顺序排序列表。我打算列出与设置的前缀相匹配的所有单词,因此必须找到任意点后面的所有单词(如二进制搜索方法所输入的)并转发。
编辑:详细资料...
字典是这样的:
dict = [
"", // offset other values to equal word length
"ai", // all length 1
"inatonuptogo", // all length 2
"catcrydogendgodhamhathit", // all length 3
"foodhackhallhandhardhatehatshavehellridewood" // all length 4
]
l
是搜索词字长,所以dict[l]
是从字典中的字一行,长度为l
。
我正在修改John Resig's binary search method,以便它匹配前缀而不是整个单词,并返回一组结果,而不是一个真值。我也在这里设置了一个限制,因为我将这个用于自动完成功能,它只需要一些返回的值,而不是所有的匹配。
如果您向我们提供代码实际执行的执行摘要,将会有所帮助。 – 2012-07-10 22:46:07