算法之【两数之和】

function sum(arr, target) {
  const res = []
  for (let i = 0; i < arr.length; i++){
    const a = target - arr[i]
    const index = arr.indexOf(a,i)
    if (index>=0) {
      res.push([arr[i], arr[index]])
    }
  }
  return res
}
const result = sum([1,2,3,4,5,6,7,8,9], 10);
console.log(
result)
posted @ 2022-06-10 08:50  下一秒钟已经不同  阅读(22)  评论(0编辑  收藏  举报