简体   繁体   English

如何找到数组中连续零序列的所有第一个索引?

[英]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.例如,如果C2 ,则该算法将查找 00,如果C3 ,则该算法将查找 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.

相关问题 如何在JavaScript中找到数组最大值的所有索引? - How can I find all indexes of the max value of an array in javascript? 如何在javascript中查找和排序数组中对象元素的索引 - How can I find and sort the indexes of the elements of an object in array in javascript 如何查找数组中所有元素出现的索引? - How to find the indexes of all occurrences of an element in array? 如何在数字数组中插入零序列? - How can I interpolate zeroes sequences in a number array? 如何遍历字母数组并返回数组中的第一个连续辅音? - How can I loop through an array of letters and return the first consecutive consonants in the array? 使用正则表达式如何查找字符串是否以正确的序列顺序包含特定序列的所有字符? - Using a Regular Expression how can i find if a string contains all the characters of a specific sequence in the correct sequence order? 如果最小值多次出现,如何找到数组的所有索引? - How do I find all indexes of smallet value of array, if the smallest value appears multiple times? 如何在平方矩阵中找到符号的连续序列 - How to find a consecutive sequence of symbols in a squared matrix 我需要计算连续零的数量并用这样的计数更新新数组。 每个这样的零块都必须单独计算 - I need to count the number of consecutive zeroes and update the new array with such count. each such block of zeroes has to be counted separately 编写一个函数,返回二进制字符串中最长的连续零序列 - Write a function that returns the longest sequence of consecutive zeroes in a binary string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM