Javascript,合并两个排序的数组:有人能告诉我为什么这没有给出正确的合并排序吗?

我尝试合并两个数组,但得到的输出是 [ 0, 3, 3, 4, 4 ]


function mergeSortedArrays(arr1, arr2) {

  var i = 0;

  var j = 0;

  var arr3 = [];


  if (arr1 === undefined || arr1.length == 0) {

    return arr2;

  }


  if (arr2 === undefined || arr2.length == 0) {

    return arr1;

  }


  while (i < arr1.length - 1 && j < arr2.length - 1) {

    if (arr1[i] < arr2[j]) {

      arr3.push(arr1[i]);

      i++;

    } else {

      arr3.push(arr2[j]);

      j++;

    }

  }

  return arr3;

}


console.log(mergeSortedArrays([0, 3, 4, 31], [3, 4, 6, 30]));

对于这个例子,我知道我没有考虑数组大小不同的情况,但这是为了以后的问题。该代码目前甚至不适用于基本情况。它不会一直迭代并在中途中断。有人可以解决这个问题。我已经解决了 while 循环,但代码仍然不起作用。


千巷猫影
浏览 127回答 3
3回答

四季花海

你犯了两个错误。在数组 not的while循环条件检查中。这不会添加两个数组的最后一个元素。lengthlength - 1当数组之一由于&&. 所以在while之后添加其他数组的剩余元素。function mergeSortedArrays(arr1, arr2) {&nbsp; var i = 0;&nbsp; var j = 0;&nbsp; var arr3 = [];&nbsp; if (arr1 === undefined || arr1.length == 0) {&nbsp; &nbsp; return arr2;&nbsp; }&nbsp; if (arr2 === undefined || arr2.length == 0) {&nbsp; &nbsp; return arr1;&nbsp; }&nbsp; while (i < arr1.length && j < arr2.length) {&nbsp; &nbsp; if (arr1[i] < arr2[j]) {&nbsp; &nbsp; &nbsp; arr3.push(arr1[i]);&nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; arr3.push(arr2[j]);&nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp; if(i === arr1.length){&nbsp; &nbsp; &nbsp; return arr3.concat(arr2.slice(j))&nbsp; &nbsp; }&nbsp; &nbsp; else if(j === arr2.length){&nbsp; &nbsp; &nbsp; return arr3.concat(arr1.slice(i))&nbsp; &nbsp; }}console.log(mergeSortedArrays([0, 3, 4, 31], [3, 4, 6, 30]));

holdtom

您的代码中缺少两件事:while 循环为i < arr1.length - 1forarr1和j < arr2.length - 1for运行arr2。这意味着它将运行到 arr1 和 arr2 的第 3 个索引。所以最后一个索引不会在循环中执行。所以你应该像i < arr1.lengthforarr1和j < arr2.lengthfor一样重构循环arr2。下一个缺少的东西,您需要为剩余数组的剩余元素添加循环。这意味着当您执行代码时,一个循环的所有元素将被推入,arr3然后另一个数组的一个元素将被遗漏,并且不会被推入arr3(对于两个数组的相同长度)和/或多个另一个数组的元素将被遗漏(对于两个数组的不同长度)。所以你需要将另一个数组的剩余元素推送到arr3.我已经添加了你的代码的重构代码。function mergeSortedArrays(arr1, arr2) {&nbsp; &nbsp; var i = 0;&nbsp; &nbsp; var j = 0;&nbsp; &nbsp; var arr3 = [];&nbsp; &nbsp; if (arr1 === undefined || arr1.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; return arr2;&nbsp; &nbsp; }&nbsp; &nbsp; if (arr2 === undefined || arr2.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; return arr1;&nbsp; &nbsp; }&nbsp; &nbsp; while (i < arr1.length && j < arr2.length) {&nbsp; &nbsp; &nbsp; &nbsp; if (arr1[i] < arr2[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr3.push(arr1[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr3.push(arr2[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; while (i < arr1.length) {&nbsp; &nbsp; &nbsp; &nbsp; arr3.push(arr1[i]);&nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; }&nbsp; &nbsp; while (j < arr2.length) {&nbsp; &nbsp; &nbsp; &nbsp; arr3.push(arr2[j]);&nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; }&nbsp; &nbsp; return arr3;}OR您可以使用扩展运算符合并两个数组,然后您可以使用 JS 的排序方法。function mergeTwo(arr1, arr2) {&nbsp; let result = [...arr1, ...arr2];&nbsp; return result.sort((a,b) => a-b);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript