【问题标题】:Return largest number from nested arrays without math.max从没有 math.max 的嵌套数组中返回最大数
【发布时间】:2019-05-13 23:49:06
【问题描述】:

我正在尝试将每个子数组中的最大数返回到一个新数组。我觉得我真的很亲近,我觉得if(temp[j] < x) {continue;} 格格不入。我究竟做错了什么?附:我知道我可能只使用 math.max() 并节省大量代码,但我正在努力适应 for 循环和数组。

  function largestOfFour(arr) {
  let newArr = [];

  for(let i = 0; i < arr.length; i++) {
    let temp = arr[i];
    let counter = 0;
    for(let j = 0; j < temp.length; j++) {
      let x = 0;
      if(temp[j] > counter) {
      counter = temp[j];
      if(counter > x) {
        x = counter;
        if(temp[j] < x) {
          continue;
        }
      }
      newArr.push(temp[j]);

      }

    }
    console.log(arr[i])
  }
  console.log(newArr);
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

【问题讨论】:

  • 请同时添加想要的结果(哪个方向?)。

标签: javascript arrays for-loop if-statement


【解决方案1】:

您是否期望[5, 27, 39, 1001] 的输出?

如果是这样,对内部循环的这些细微调整就可以完成工作:

function largestOfFour(arr) {
    let newArr = [];

    for(let i = 0; i < arr.length; i++) {
        let temp = arr[i];
        let x = 0;
        for(let j = 0; j < temp.length; j++) {

            if(temp[j] > x) {
                x = temp[j];
            }
        }

        newArr.push(x);
        //console.log(arr[i])
    }
    console.log(newArr);
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

您在正确的轨道上,但您希望避免将子数组的最大值推到newArr,直到内部循环完成对该子数组的迭代。在完成迭代之前,您不知道子数组的哪个值最高。

【讨论】:

  • 我的面试会失败,如果最大数字不大于零怎么办?初始化应该基于第一个索引,而不是 0。
  • 完美,谢谢!在我尝试添加 if(counter > x) { x = counter; 之前,这就是我的答案。 if(temp[j]
  • @epascarello 这是我最初的策略,我以后一定会这样做,感谢您的意见。
  • @epascarello 我假设数据集是正整数。我还假设父数组只包含数组,而那些子数组只包含数字,而不是字符串、对象等。这里的重点是帮助 OP 在他的代码中进行下一个小改进,了解他做错了什么,并学习。我的回答不是抓住每一个可能的边缘情况,或者用更高效/优雅的东西完全重写他的代码。
【解决方案2】:

@Elliot B. 的答案可能是您正在寻找的作为哪里出错的解释。

在数组上使用 Map/Reduce 的替代解决方案

从这里获取的解决方案:https://medium.freecodecamp.org/three-ways-to-return-largest-numbers-in-arrays-in-javascript-5d977baa80a1

function largestOfFour(mainArray) {
  return mainArray.map(function (subArray){
    return subArray.reduce(function (previousLargestNumber, currentLargestNumber) {
      return (currentLargestNumber > previousLargestNumber) ? currentLargestNumber : previousLargestNumber;
    }, 0);
  });
}
//largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);


const result = largestOfFour([
  [4, 5, 1, 3],
  [13, 27, 18, 26],
  [32, 35, 37, 39],
  [1000, 1001, 857, 1]
]);

console.log(result);

【讨论】:

  • 最好省略初始值设定项 ("0") 以使其适用于负数。
  • 是的,如果没有提供,默认情况下它应该将0 作为值
  • 不,如果您不提供 init 值,reduce 会将第一个元素作为 init 并从第二个元素开始。
【解决方案3】:

在你的代码中,你需要在内循环中获取最大的数,然后push它:

function largestOfFour(arr) {
  let newArr = [];

  for (let i = 0; i < arr.length; i++) {
    let temp = arr[i];
    let res = -Infinity; // initialize the res with lowest number
    for (let j = 0; j < temp.length; j++) {
      if (temp[j] > res) { // if a number is greater than res, assign it to res
        res = temp[j];
      }
    }
    newArr.push(res); // push res to the result arr
  }
  return newArr;
}

const result = largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

console.log(result);

如果您将最大逻辑重构为外部函数会更容易:

const getArrayMax = ([first, ...arr]) => {
  let mx = first;
  
  for (const cur of arr)
    if (cur > mx)
      mx = cur;
  
  return mx;
}

function largestOfSubarrays(arr) {
  const newArr = [];

  for (let i = 0; i < arr.length; i++) {
    newArr.push(getArrayMax(arr[i])); // push res to the result arr
  }
  
  return newArr;
}

const result = largestOfSubarrays([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

console.log(result);

【讨论】:

  • 我没有意识到这是 FCC 基本算法挑战(我碰巧所在的部分),所以我尝试了我的解决方案,但它不适用于 -1。您初始化为 -Infinity 的策略现在很有意义,谢谢!
  • 你可以简单地用temp[0]初始化并从1开始循环。
  • @georg 这就是我最初所做的,但我觉得它看起来不对哈哈。
  • 这与重构为两个函数和解构很相配。请参阅更新的第二个答案。
【解决方案4】:

您也可以简单地按desc 顺序对数组进行排序并获取第一个元素:

const largestOfFour = arr => arr.map(x => x.sort((a,b) => b-a)[0])

console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]))

【讨论】:

    猜你喜欢
    • 2019-02-07
    • 2022-01-11
    • 2017-12-05
    • 2016-03-11
    • 2015-10-13
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多