我想编写一个函数,从给定的起始索引开始在给定的数组中查找连续的子数组,如果找到则返回数组中子数组的索引,如果找不到则返回 -1。这类似于 String.indexOf
,但用于数组和子数组而不是字符串和子字符串。
这是我的工作代码:
var find_csa = function (arr, subarr, from_index) {
if (typeof from_index === 'undefined') {
from_index = 0;
}
var i, found, j;
for (i = from_index; i < 1 + (arr.length - subarr.length); ++i) {
found = true;
for (j = 0; j < subarr.length; ++j) {
if (arr[i + j] !== subarr[j]) {
found = false;
break;
}
}
if (found) return i;
}
return -1;
};
这些是我的测试及其预期值:
console.log(find_csa([1, 2, 3, 4, 5], [2, 3, 4]) === 1);
console.log(find_csa([1, 2, 3, 4, 5], [5]) === 4);
console.log(find_csa([1, 2, 3, 4, 5], [1, 3]) === -1);
console.log(find_csa([1, 2, 3, 4, 5], [42]) === -1);
console.log(find_csa([1, 2, 3, 4, 5], []) === 0);
console.log(find_csa([3, 4, 3, 4, 3, 4], [3, 4, 3], 1) === 2);
console.log(find_csa([6, 6, 6, 7], [6, 6, 7]) === 1);
console.log(find_csa([12, 9, 16, 42, 7, 866, 3], [16, 42, 7, 866]) === 2);
我的代码通过了测试,但如您所见,它在内部循环中使用了一个 bool 值 found
环形。有没有更简洁的写作方式?我调查了Array.prototype.findIndex
但它目前是一项实验技术,所以我不能使用它。我想要一种适用于大多数浏览器的方法。我知道 Mozilla 页面上写了一个“polyfill”代码片段,但它比我当前的代码还要长,而且由于函数调用,它会更慢,所以我宁愿避免它。
我对这个函数的主要目标是性能(子数组会非常小,所以我认为使用 Boyer-Moore string search algorithm 或 tries 有点矫枉过正),然后我的次要目标是优雅 我的实现。考虑到这两个目标,我想知道是否有更好的方法来编写这段代码,或者是否有任何我遗漏的 JavaScript 特性或函数可以帮助我避免 found
bool 值。
JSFiddle 如果它对任何人有帮助:http://jsfiddle.net/qc4zxq2p/
最佳答案
Are there any JavaScript features or functions that I'm missing that could help me avoid the
found
boolean
是的,您可以使用 label在你的外循环上:
function find_csa(arr, subarr, from_index) {
var i = from_index >>> 0,
sl = subarr.length,
l = arr.length + 1 - sl;
loop: for (; i<l; i++) {
for (var j=0; j<sl; j++)
if (arr[i+j] !== subarr[j])
continue loop;
return i;
}
return -1;
}
关于javascript - 在 JavaScript 中的数组中查找连续子数组的优雅方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29425820/