【问题标题】:Write a function that takes in an array of integers, and a string that will be either 'even' or 'odd'编写一个函数,该函数接收一个整数数组和一个“偶数”或“奇数”字符串
【发布时间】:2021-03-30 16:08:38
【问题描述】:

我正在处理 javascript 中的一个问题,我应该编写一个函数,该函数接收一个整数数组和一个“偶数”或“奇数”字符串。该函数将计算连续出现 4 个偶数或 4 个奇数的次数。

例如:

quadruples([3,2,2,4,8,5], 'even')  // 1
quadruples([2,4,6,8,10,5], 'even')  // 2
quadruples([2,4,6,8,10,5], 'odd')  // 0

到目前为止,这是我所在的位置:

function quadruples(givenArray, evenOrOdd) {
  let arr = []
  if(evenOrOdd == 'even') {
    if( i = 0; i < givenArray.length; i++) {
      
    }
};

我想我需要运行一个 for 循环,然后使用 % 运算符,但我不知道从哪里开始。

感谢任何帮助!

【问题讨论】:

  • 那么如果数组是[1,3,5,7,9,6,3,9,11,15],输出会是什么(奇数evenOrOdd)?
  • 我更新了帖子..例如: quadruples([3,2,2,4,8,5], 'even') // 1 quadruples([2,4,6,8 ,10,5], '偶数') // 2 个四倍数([2,4,6,8,10,5], '奇数') // 0
  • 我明白,但在我提供的具体情况下,输出会是什么?根据您的解释,它可以是 2 或 3。
  • 如果这是家庭作业,请查看How do I ask and answer homework questions? 尤其是上面写着“询问您现有实施的具体问题”的部分。
  • 我只是想弄清楚从这里去哪里..我查看了文档,但不确定如何实现 %

标签: javascript arrays for-loop modulus


【解决方案1】:

您需要使用局部和全局变量对此进行动态编程: [2、4、6、8、10、5]

  • 2 - 偶数,count 为 1,totalCount 为 0
  • 4 - 偶数,计数为 2,totalCount 为 0
  • 6 - 偶数,计数为 3,totalCount 为 0
  • 8 - 偶数,计数为 4,totalCount 为 0
  • 10 - 偶数,计数为 5,totalCount 为 0
  • 5 - 奇数,计数为 5,将 totalCount 增加 5 - 4 + 1 = 2,将计数重置为 0

const quadruples = (givenArray, evenOrOdd) => {
  // never hardcode `magic numbers`, create constants for them
  const sequenceLength = 4

  // based on evenOrOdd calculating what the division by 2
  // will be if it is even, then 0, if it is odd, then 1
  const rest = evenOrOdd === 'even' ? 0 : 1

  // this will hold the total count of quadruples
  let totalCount = 0

  // this is the local count of contiguous elements
  let count = 0

  // looping over the array
  for (let i = 0; i <= givenArray.length; i += 1) {
    const el = givenArray[i]

    // if the element is not what we want
    if (i === givenArray.length || el % 2 !== rest) {
      // if the count is 4 or more, we add to totalCount the count
      // minus 4 and plus 1, meaning that if we have 4, it's 1 quadruple,
      // if it is 5, then it's 2 quadruples, etc.
      // Otherwise (count is less than 4) we add 0 (nothing)
      totalCount += count >= sequenceLength ? count - sequenceLength + 1 : 0

      // resetting the count to zero as we encountered the opposite
      // of what we are looking for (even/odd)
      count = 0

      // if the element is what we need (even or odd)
    } else {
      // increasing the count of how many we've seen by far
      count += 1
    }
  }

  // returning totalCount of quadruples
  return totalCount
}

console.log(quadruples([1, 3, 5, 7, 9, 11], 'odd')) // 3
console.log(quadruples([3, 2, 2, 4, 8, 5], 'even')) // 1
console.log(quadruples([2, 4, 6, 8, 10, 5], 'even')) // 2
console.log(quadruples([2, 4, 6, 8, 10, 5], 'odd')) // 0

【讨论】:

  • 这行得通,除非我添加额外的输入
  • 你能给我额外的输入和你期望的输出吗?
  • 是的,你是对的,我发现了一个错误。当我们寻找偶数/奇数四元组时,如果最后一个元素是偶数/奇数,则计算不正确。我修复了代码。现在对你有用吗?
  • 是的..我在看那个..感谢您的帮助!
【解决方案2】:

我写这个递归。

console.log(quadruples([3, 2, 2, 4, 8, 5], 'even')); // 1
console.log(quadruples([2, 4, 6, 8, 10, 5], 'even')); // 2
console.log(quadruples([2, 4, 6, 8, 10, 5], 'odd')); // 0
console.log(quadruples([5, 4, 6, 8, 10, 5, 2, 2, 2, 2, 4, 4], 'even')); // 4

function quadruples(givenArray, evenOrOdd) {
  const maxSequence = 4;
  let result = 0;
  if (givenArray.length < maxSequence)
    return 0;
  for (let i = 0; i < maxSequence; i++) {
    if (givenArray[i] % 2 != (evenOrOdd == "even" ? 0 : 1)) {
      givenArray = givenArray.slice(i + 1);
      return (result += quadruples(givenArray, evenOrOdd));
    }
  }
  result++;
  givenArray = givenArray.slice(1);
  return (result += quadruples(givenArray, evenOrOdd));
}

【讨论】:

  • 不错的解决方案 :) 它需要 O(n) 内存,而迭代解决方案只需要两个指针,因此需要 O(1) 内存。无论如何,如果您将var 更改为let 并引入const four = 4 而不是到处使用4,我保证会支持您的解决方案:)
  • 是的,你是对的,我只是试试这个练习 :))它是 es5 解决方案 :))
  • 你在哪里定义的数组?
  • 他在他的解决方案中将givenArray重命名为array,所以它是一个函数参数
猜你喜欢
  • 1970-01-01
  • 2017-06-17
  • 2021-07-22
  • 2022-12-04
  • 2016-04-11
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 2011-06-10
相关资源
最近更新 更多