美文网首页
在数组中找到两数之和为 target --

在数组中找到两数之和为 target --

作者: Joah_l | 来源:发表于2019-11-21 11:07 被阅读0次
function twoSum2(nums, target) {
  // 查找 hash table
  let obj = {}
  for (let i = 0, len = nums.length; i < len; i++) {
    let item = nums[i]
    let j = target - item
    if (typeof obj[j] !== 'undefined') {
      return [obj[j], i]
    }
    obj[item] = i
  }
  return ''
}

const r = twoSum2([1, 3, 4, 2, 1], 2)
console.log(r)

相关文章

  • hashmap应用

    两数之和问题 题目描述:在给定的数组nums中找到两个数之和等于目标值target。 1. 暴力方法 检索数组中所...

  • 在数组中找到两数之和为 target --

  • 2022-04-03 LeetCode刷题

    两数之和 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target...

  • ARTS-Week 1

    Algorithm 两数之和 题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为...

  • leetcode 两数之和

    leetcode 两数之和 思路 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目...

  • leecode刷题(8)-- 两数之和

    leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,...

  • LeetCode(PHP)之两数之和

    题目:两数之和(Two Sum) 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目...

  • 2019-05-12

    # 1. 两数之和 ###描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目...

  • leetcode----两数之和

    leetcode每月一题 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为...

  • 经典算法 — 两数之和

    经典算法 — 两数之和 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目...

网友评论

      本文标题:在数组中找到两数之和为 target --

      本文链接:https://www.haomeiwen.com/subject/qyizictx.html