我怎麼會寫這樣的代碼,而所有的重複刪除重複的代碼
// 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