【问题标题】:How to check whether the sum of any two integers in an array exists as an item in the array?如何检查数组中任意两个整数的和是否作为数组中的一项存在?
【发布时间】:2020-12-01 14:02:06
【问题描述】:

我正在编写一个函数,如果数组中任意两个整数的和作为数组中的一项存在,则该函数返回 true。例如:

[3, 4, 8, 11]
//returns true because 8 + 3 = 11 

[2, 4, 7, 12]
//returns false 

到目前为止,我得到了这个:

const checkArray = (arr) => {
  arr.sort((a, b) => a - b);
  let lhs = 0;
  let rhs = array.length - 1;

  while (lhs < rhs) {
    const sum = arr[lhs] + arr[rhs];

    if (arr.includes(sum)) {
      return true;
    } else if (arr[lhs] < sum) {
      lhs++;
    } else {
      rhs--;
    }
  }
  return true;
}

搞了一段时间,不知道我做错了什么。

【问题讨论】:

  • 您是否尝试过调试一个小的失败示例?你的发现在哪里?
  • return false; 应该是默认值
  • 应该是arr.length -1 而不是array.length - 1
  • 我不明白算法背后的想法,而且它不起作用。你能详细说明一下吗?
  • 经过一番研究,您的代码看起来像是陈旧的 quadratic 3SUM implementation 的剥离/破坏版本。我不确定这实际上是您的代码,还是链接代码的任意破坏版本,没有任何关于源的解释,或者编辑背后的想法(不起作用)。自发布此消息以来,您似乎也死了。

标签: javascript arrays algorithm


【解决方案1】:

我尝试不使用排序:

const checkArray = (arr) => {
  if (arr.length < 2) return false;
  let lhs = 0;
  while (lhs < arr.length - 2) {
    let rhs = lhs + 1;
    while (rhs <= arr.length - 1) {
      if (arr.includes(arr[lhs] + arr[rhs])) {
        return true;
      }
      rhs += 1;
    }
    lhs += 1;
  }
  return false;
};

【讨论】:

    【解决方案2】:

    Array.prototype.some()Array.prototype.find() 一起使用

    const checkArray = (array) => {
        //Wll only return true if the inner array.some returns true
        return array.some((num, index) => {
            return array.some((innernum, innerIndex) => {
                //Will return true if current number is === to any two numbers
                return innerIndex !== index && !!array.find(item => item === (innernum + num))
            })
        });
    }
    

    【讨论】:

      【解决方案3】:

      您可以采用嵌套方法,其中一个值和一个总和用于生成 mssing 值。

      function check(array) {
          const
              need = {};
      
          for (let i = 0; i < array.length - 1; i++) {
              if (need[array[i]]) return true;
              for (let j = array.length - 1; j > i + 1; j--) {
                  need[array[j] - array[i]] = true;
              }
          }
          return false;
      }
      
      console.log(check([1, 2, 3]));     //  true
      console.log(check([3, 4, 8, 11])); //  true
      console.log(check([2, 4, 7, 12])); // false

      【讨论】:

        【解决方案4】:

        试试这个代码...

        //const arr = [3,4,8,11];
        //const arr = [2, 4, 7, 12];
        
        const checkArray = (arr) => {
           var a = [];
           for(var i=0; i<arr.length-1; i++){
              for(var j=i+1; j<arr.length; j++){
                  a.push(arr[i]+arr[j]);
               }
            }
            for(var x of arr){
               if(a.indexOf(x) != -1){
                   return true;
                }
             }
          return false;
        }
        
        console.log(checkArray(arr));
        

        如果此代码有帮助,请告诉我...!

        【讨论】:

          猜你喜欢
          • 2021-05-15
          • 2015-09-19
          • 2011-01-16
          • 1970-01-01
          • 1970-01-01
          • 2017-05-22
          • 2013-10-29
          • 2015-04-16
          • 1970-01-01
          相关资源
          最近更新 更多