Skip to content

Latest commit

 

History

History
31 lines (28 loc) · 644 Bytes

2020-11-19-1.md

File metadata and controls

31 lines (28 loc) · 644 Bytes
  1. 两数之和

https://leetcode-cn.com/problems/two-sum/

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    let map = {}
    for(let i=0;i<nums.length;i++) {
        let getIndex = map[target - nums[i] /* map[7] */]
        if(getIndex !== undefined) {
            return [getIndex, i]
        }else {
            map[nums[i]] = i
        }
    }
};
/**
 * map = {
 *  '2':0,
 *  '7':1
 * }
 * 先找了 map[7],此时是undefined,然后第二个循环,找map[2] 因为第一轮的时候已经放入了 所以返回map[2]的值以及当前index
 * 
 */