代码之家  ›  专栏  ›  技术社区  ›  Nick Kinlen

javascript:检查数组中是否有使数组连续所需的缺失数字

  •  0
  • Nick Kinlen  · 技术社区  · 6 年前

    在代码信号方面进行一些javascript挑战,我有一个问题要解决:

    拉蒂奥格收到了不同大小的雕像作为码主的生日礼物,每个雕像都有一个非负整数大小。因为他喜欢把东西做得完美,所以他想把它们从小到大排列,这样每尊雕像都会比前一尊大1倍。他可能需要一些额外的雕像才能做到这一点。帮助他计算出所需额外雕像的最小数量。 例子 对于status=[6,2,3,8],输出应该是 makeArrayConsecutive2(雕像)=3。 拉蒂奥格需要4号、5号和7号的雕像。

    我的方法:

    • 将数组从小到大排序
    • 创建计数器变量以存储丢失的数字数
    • 遍历数组
    • 从[I]元素中减去[I+1]元素
    • 如果等于1,则数字是连续的,如果不是,则数字不是连续的(递增计数器变量)
    • 返回计数器变量

    这是我的代码:

    function makeArrayConsecutive2(statues) {
        // Sorts array numerically smallest to largest
        statues.sort((a, b) => a - b);
    
        let counter = 0;
    
        // If array only contains one number return 0
        if(statues.length === 1) {
            return 0;
        }
    
        /* Iterate through array, subtract the current element from the next element, if it 
           equals 1 the numbers are consecutive, if it doesn't equal one increment the counter 
           variable */
        for(let i = 0; i <= statues.length -1; i++) {
            if(statues[i] !== statues.length -1 && statues[i + 1] - statues[i] != 1) {
               counter++;
            }
    
           console.log(statues[i]);
           console.log('counter : ' + counter);
        }
    
        return counter;       
    }
    

    什么时候? statues 包含 [5, 4, 6] 结果是:

    4
    counter : 0
    5
    counter : 0
    6
    counter : 1
    

    我认为问题在于数组在最后一个元素上,在本例中是6,它试图查看 statues[i + 1] 当该元素不存在时。我补充说 statues[i] !== statues.length -1 我的if语句来解决这个问题,但它似乎不起作用。我的代码有什么问题,为什么最后一个元素会递增计数器变量?

    2 回复  |  直到 6 年前
        1
  •  2
  •   danh    6 年前

    我将通过构建一个目标数组来接近它,这个数组从输入的min+1到max-1除以1,不包括输入的成员…..

    function missingConseq(input) {
      let min = Math.min.apply(null, input)
      let max = Math.max.apply(null, input)
      let result = []
    
      for (i = min+1; i < max; i++) {
        if (!input.includes(i)) result.push(i)
      }
      return result
    }
    
    let array = [6, 2, 3, 8]
    console.log(missingConseq(array))
        2
  •  0
  •   Nikhil    6 年前

    你只需要换衣服

    if(statues[i] !== statues.length -1) if(i !== statues.length -1)

    要检查当前索引是否是最后一个索引,而不是当前索引处的值是否等于最后一个索引。这就是意外输出的原因。

    工作示例:

    function makeArrayConsecutive2(statues) {
        // Sorts array numerically smallest to largest
        statues.sort((a, b) => a - b);
    
        let counter = 0;
    
        // If array only contains one number return 0
        if(statues.length === 1) {
            return 0;
        }
    
        /* Iterate through array, subtract the current element from the next element, if it 
           equals 1 the numbers are consecutive, if it doesn't equal one increment the counter 
           variable */
        for(let i = 0; i <= statues.length -1; i++) {
            if(i !== statues.length -1 && statues[i + 1] - statues[i] != 1) {
               counter++;
            }
    
           console.log(statues[i]);
           console.log('counter : ' + counter);
        }
    
        return counter;       
    }
    
    makeArrayConsecutive2([5, 4, 6]);

    您也可以更改 for 循环如下以执行相同的操作。

    for(let i = 0; i < statues.length -1; i++) {
      if(statues[i + 1] - statues[i] != 1) {
        counter++;
      }
    
      console.log(statues[i]);
      console.log('counter : ' + counter);
    }