【问题标题】:How does one, within a sequence of digits, count how many times a digit appears thats value is exactly one less than the previous digit's one?在一个数字序列中,如何计算一个数字出现多少次,该值恰好比前一个数字的值小一?
【发布时间】:2021-01-20 13:56:20
【问题描述】:

代码:

function OneDecremented(num) { 
  num = num.toString()
  var count = 0

  for(i = 1; i < num.length; i++) {
    if(num[i - 1] - num[i] === 1){
      count++
    }
  }
  return count

}
console.log(OneDecremented(9876541110))

所以我很难理解两件事:

  1. i 和 num[i] 有什么区别
  2. 我不明白 if 语句中的计算是如何发生的,有人可以分解一下吗?

很抱歉,如果这些问题听起来太愚蠢,我是 JS 新手,无法真正理解算术计算。谢谢你的时间。

【问题讨论】:

  • 欢迎来到 SO,很高兴您得到了成功的答案。对任何花时间通过回答提供帮助的其他用户表示感谢/投票是很礼貌的。
  • @Mitya ... OP 没有足够的投票声誉(甚至被否决了一次);但让我来帮忙。
  • @PeterSeliger 谢谢,非常好。我没有意识到(即使经过这么长时间)菜鸟还不能投票。这似乎很奇怪;我认为应该鼓励他们对给出的答案做出回应(除了接受答案)。
  • @Mitya 我试过了,但它不允许我这样做。感谢您的宝贵时间
  • 别担心 - 我没有意识到你不能作为菜鸟投票。

标签: javascript arrays algorithm for-loop reduce


【解决方案1】:

这段代码写得不好,但最重要的是,它在全球范围内泄露了 i 引用,所以,让我们从一个更好的版本开始:

function OneDecremented(num) {
  var str = num.toString();
  var count = 0;
  for(var i = 1; i < str.length; i++) {
    if(str[i - 1] - str[i] === 1)
      count++;
  }
  return count;
}

字符串,在现代JS中,可以像数组一样访问,索引返回index位置的char:

if(str[i - 1] - str[i] === 1)
// is the same as
if ((str.charAt(i - 1) - str.charAt(i)) === 1)

一旦检索到每个字符,代码就会进行隐式“字符到数字”转换,这要归功于- 运算符,但如果它是+,它会连接起来这两个字符改为字符串(所以,要小心)。

明确一点总是更好,但是如果您知道- 的工作原理,它就可以完成这项任务。

循环从1 开始,它检查i - 1 处的字符,在第一次迭代中,索引0 处的字符减去当前字符,是1,即当前字符比前一个少一个

在这种情况下,计数器会累加。

【讨论】:

  • 感谢您的解释!现在更有意义了:)
【解决方案2】:

Andrea 和 Mitya 已经成功了。

下一步可能是切换到基于first class 的方法,例如使用特定的Array 方法,例如reduce

如果实施正确,这种方法通常会提高代码的可读性/可维护性,并允许更好的代码重用。

对于 OP 提供的示例,可以编写两个函数,即获取计数的实际方法和上述第一类 reducer 功能。由于reduce 是处理数组的标准方式,因此reducer/callback 的参数优先级也得到了很好的指定...

[/* ... */].reduce(function(accumulator, currentValue, currentIndex, currentlyProcessedArray) {

  // implement reducer/aggregation/accumulator logic here.

  // the return value serves as the
  // new `accumulator` value within
  // the next iteration step.

  // thus, always return something! ... e.g ...

  return (accumulator + currentValue); 
});

function aggregatePrecursorAndDecrementedSuccessorCount(count, char, idx, arr) {

  const precursorValue = Number(arr[idx - 1]);
  const incrementedCurrentValue = (Number(char) + 1);
  const isValidCount = (precursorValue === incrementedCurrentValue);

  return (count + (isValidCount ? 1 : 0));
//return (count + Number(isValidCount)); // cast boolean value to either 1 or 0.
}

function getPrecursorAndDecrementedSuccessorCount(int) {
  return String(int) // - assure/typecast always a/into string value.
    .split('')       // - split string value into an array of single characters.
    .reduce(aggregatePrecursorAndDecrementedSuccessorCount, 0);
}

console.log(getPrecursorAndDecrementedSuccessorCount(9876541110));
.as-console-wrapper { min-height: 100%!important; top: 0; }

【讨论】:

  • 感谢您向我介绍不同的方法!真的有帮助:)
【解决方案3】:

i 和 num[i] 有什么区别

i 是迭代键,即 0、1、2 等,因为字符串化数字中有尽可能多的字符。 num[i] 是字符串中索引i 处的字符,即num[i],其中i 为0 == 9(字符串中索引0 处的字符)。

我不明白 if 语句中的计算是如何发生的,有人可以分解一下吗?

也就是说:如果计算字符串的索引i-1 处的数字减去当前考虑的数字(字符串中的索引i)减去1,则增加count

按实际使用的数量逐步计算:

  • 9 - 没有前一个字符;计算 (undefined - 9) 不等于 1
  • 8 - 前一个字符为 9; (9 - 8) == 1;递增count
  • 7 - 同上
  • 6 - 同上
  • 5 - 同上
  • 4 - 同上
  • 1 - 前一个字符为 4;计算 (4 - 1) 不等于 1
  • 1 - 前一个字符为 1;计算 (1 - 1) 不等于 1
  • 1 - 同上
  • 0 - 前一个字符为 1; (1 - 0) == 1;递增count

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 2017-08-09
    • 2019-02-16
    • 1970-01-01
    相关资源
    最近更新 更多