📜  JS 数组中两个数字之和等于给定数字 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:29.445000             🧑  作者: Mango

代码示例1
let twoSum = (array, sum) => {
    let hashMap = {},
      results = []

        for (let i = 0; i < array.length; i++){
            if (hashMap[array[i]]){
                results.push([hashMap[array[i]], array[i]])
            }else{
                hashMap[sum - array[i]] = array[i];
            }
          }
          return results;
    }
console.log(twoSum([10,20,10,40,50,60,70,30],50));