ZhuangER
10/1/2018 - 4:08 AM

[1. Two Sum] #tags: leetcode

[1. Two Sum] #tags: leetcode

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
  const map = {};
  let result = [];
  for (let i = 0; i < nums.length; ++i) {
    map[nums[i]] = i;
  }
  
  for (let i = 0; i < nums.length; ++i) {
    const other = target- nums[i];
    if (map[other] !== undefined && map[other] !== i) {
      result = [i, map[other]];
      break;
    }
  }
  
  return result;
};