首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >免费代码营-成对

免费代码营-成对
EN

Code Review用户
提问于 2015-07-29 11:03:08
回答 7查看 1.9K关注 0票数 8

我正在通过免费代码营教学大纲和中级JavaScript算法。这个成对的问题是该部分的最后一个挑战。本节是在“面向对象的JavaScript”之后出现的。所以我想他们是在寻找一个面向对象的解决方案,但是说明中包含了一个指向MDN的array.reduce()的链接。我的解决方案不使用array.reduce(),我非常希望能得到一些反馈,说明我本可以做得更好,使我的代码更加紧凑和高效。感觉有点笨重,但通过了所有的测试。

指令

返回可以与另一个元素配对的“arr”元素的所有索引的和,以形成与第二个参数'arg‘中的值相等的和。如果可能有多个和,则返回最小和。一旦使用了一个元素,它就不能被重用来与另一个元素配对。例如,配对(1,4,2,3,0 5,7)应该返回11,因为4、2、3和5可以配对成相等的7。配对(1,3,2,4,4)只等于1,因为只有前两个元素可以配对为等于4,而第一个元素的索引是0!如果你被困住了,记得使用RSAP。试着配对程序。写你自己的代码。下面是一些有用的链接: Array.reduce()

我的解决方案

代码语言:javascript
运行
复制
function pairwise(arr, arg) {
    this.objects = [];
    var total = 0;

    function Element(value, index) {
        this.value = value;
        this.index = index;
        this.used = 0;
    }

    for (var i = 0; i < arr.length; i++) {
        this.objects.push(new Element(arr[i], i));
    }

    for (var j = 0; j < objects.length; j++) {
        if (objects[j].used === 0) {
            for (var k = 0; k < objects.length; k++) {
                if (objects[k].used === 0 && objects[k].index != objects[j].index) {
                    if (arg - objects[j].value == objects[k].value) {
                        total = total + objects[j].index + objects[k].index;
                        objects[j].used = 1;
                        objects[k].used = 1;
                        break;
                    }
                }
            }
        }
    }

    return total;
}

pairwise([1,1,1], 2);
EN

回答 7

Code Review用户

回答已采纳

发布于 2015-07-31 09:03:56

我查看了您的代码,它是一个有效的解决方案,但是您可以通过更好地利用JavaScript已经提供的功能(如Array.prototype.indexOf() )来减少代码库。

例如,我没有构建一个新的类函数(Element)来跟踪某个索引的外观,我只是对初始数组进行了深度复制,并使用indexOf()对其进行了解析。

此外,在代码中,当您第一次声明this.objects = []时,this实际上是指全局范围(window object)。如您所见,在不构建新实例(new关键字)的情况下,您是按对调用。在这种情况下,this关键字被绑定到全局窗口对象。

请在下面找到我对它的看法:

代码语言:javascript
运行
复制
function pairwise(arr, arg) {

  var result = 0,
      newArr = [],
      //Used to hold the indices that we have already used to form our sum
      indices = [];

  //Loop through arr and create a deep copy of it in newArr
  for(var k = 0; k < arr.length; k++) {
    newArr.push(arr[k]);
  }

  //Loop through arr
  for(var i = 0; i < arr.length; i++) {

    //Loop through newArr
    for(var j = 0; j < newArr.length; j++) {
      //Since we want to add different elements of the array, we want to avoid adding the same element
      if(i !== j) {
        //If the sum of two elements is equal to arg AND the indices that we have in i and j are not part of the indices array
        //Indices array is used to hold the already used indices, thus ensuring the accurate parsing of the parameters
        if(arr[i] + newArr[j] === arg && indices.indexOf(i) === -1 && indices.indexOf(j) === -1) {
          //Sum the indices up
          result += i + j;
          //Push the indices in the indices array in order to not use them in further iterations
          indices.push(i, j);
        }
      }
    }
  }

  return result;
}

pairwise([1,4,2,3,0,5], 7);
票数 3
EN

Code Review用户

发布于 2015-08-03 15:00:32

弗拉德Z的答案是正确的,但在这个问题上,自由环保公司有奇怪的措辞。我用了一个类似的答案,但在这个测试中失败了:

代码语言:javascript
运行
复制
expect(pairwise([0, 0, 0, 0, 1, 1], 1)).to.equal(10);

我的问题和Vlad解决方案的问题是,0,1 ->索引(0和4)和0,1索引(1和5)都是可以接受的,应该返回10。

我会使用Vlad的解决方案,但是在这个函数中,而不是在indexOf === -1中来检查是否已经存在一对:

代码语言:javascript
运行
复制
function checkPairExists(value,position,pairsArray){
for(var i = 0; i < pairsArray.length; i++){
   if (pairsArray[i].value === value && pairsArray[i].position === position){
     return true;
   }
}
 return false;
}
票数 2
EN

Code Review用户

发布于 2016-07-13 10:19:54

这个函数的实现使用“减少”方法来得到索引的总和,并且它完成了所有的免费代码营测试。

代码语言:javascript
运行
复制
function pairwise(arr, arg) {
  return arr.reduce((sum, value1, index1) => {
    arr.slice(index1 + 1).forEach((value2, index2) => {
      if (arr[index1] + arr[index1 + 1 + index2] === arg) {
        arr[index1] = arr[index1 + 1 + index2] = NaN;
        sum += index1 + index1 + 1 + index2;
      }
    });
    return sum;
  }, 0);
}
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/98448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档