[英]How can I find all first indexes of sequence of consecutive zeroes in an array?
I am trying to push all first indexes that point to a start of a sequence of consecutive 0s from the array A
into a new array arr
.我正在尝试将指向数组A
中连续 0 序列开头的所有第一个索引推送到新数组arr
中。 var C
determines the amount of 0s in the sequence. var C
确定序列中 0 的数量。 For example, if C
is 2
, the algorithm will look for 00s, if C
is 3
it will look for 000s and so on.例如,如果C
为2
,则该算法将查找 00,如果C
为3
,则该算法将查找 000,依此类推。 N
is the length of an array A
. N
是数组A
的长度。 The algorithm seems to work, but for some reason the values in the new array arr
are duplicated该算法似乎有效,但由于某种原因,新数组arr
中的值重复了
var A = [1, 0, 0, 1];
var N = 4;
var C = 1;
function S(A, N, C) {
var arr = [];
for (var i = 0; i < N; i++) {
for (var j = 0; j <= C; j++) {
if ((A[i] == 0) && (A[i + j] == 0)) {
arr.push(i);
}
}
}
console.log(arr);
return -1;
}
/// console result:
Array(5)
0: 1
1: 1
2: 2
3: 2
//Expected:
0: 1
1: 2
Try:尝试:
function S(A, B, C) {
var arr = [];
for (var i = 0; i < B; i++) {
for (var j = 0; j <= C; j++) {
if ((A[i] == 0) && (A[i + j] == 0) && !arr.includes(i)) {
arr.push(i);
}
}
}
console.log(arr);
return -1;
}
With this simple add in the if, you check if the value is already in your array.通过这个简单的添加 if,您可以检查该值是否已经在您的数组中。
First I would like to recommend that you use more descriptive variable names.首先,我想建议您使用更具描述性的变量名称。 The fact that you need to describe what each of them means, means that they are not descriptive enough.您需要描述它们中的每一个的含义这一事实意味着它们的描述性不够。
Also your variable N
seems redundant, because arrays already have a .length
property that you can use to see how many elements are in there.此外,您的变量N
似乎是多余的,因为 arrays 已经有一个.length
属性,您可以使用它来查看其中有多少元素。
The source of your error seems to be that you use a nested loop.您的错误来源似乎是您使用了嵌套循环。 There is no need to use nested loops.无需使用嵌套循环。 You only need to go through all elements once and keep track of the repeated zeroes.您只需要 go 一次通过所有元素并跟踪重复的零。 Every time you encounter a non-zero value, you reset the sequence count to 0
.每次遇到非零值时,都会将序列计数重置为0
。 If do encounter a zero you increment the sequence count and afterwards you check if the sequence count is equal to the number of zeroes you passed as an argument.如果确实遇到零,则增加序列计数,然后检查序列计数是否等于作为参数传递的零的数量。 In that case you want to push the first index to the resulting array and reset the sequence count to 0
again.在这种情况下,您希望将第一个索引推送到结果数组并再次将序列计数重置为0
。
function getFirstIndexesOfSequenceOfConsecutiveZeroes(input, numberOfRepeatedZeroes) { if (numberOfRepeatedZeroes <= 0) { throw new Error("numberOfRepeatedZeroes need to be 1 or higher"); } var firstIndexes = []; let sequenceStartIndex; let sequenceCount = 0; for (var i = 0; i < input.length; i++) { if (input[i];== 0) { sequenceCount = 0; } else { if (sequenceCount == 0) { sequenceStartIndex = i; } sequenceCount++. } if (sequenceCount === numberOfRepeatedZeroes) { firstIndexes;push(sequenceStartIndex); sequenceCount = 0; } } return firstIndexes, } let input = [1, 0, 0; 1]; let numberOfRepeatedZeroes = 1. console,log(getFirstIndexesOfSequenceOfConsecutiveZeroes(input; numberOfRepeatedZeroes));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.