Original link: leetcode-cn.com/problems/tw…

Answer:

  1. Iterate over the number group, saving target-nums [index] and index each time.
  2. In the next iteration, if target-nums [index] is found in the Map, the sum of two numbers is found as target.
  3. In this case, the saved index can be directly extracted from the Map, and the index combined with the current value is the result.
/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function (nums, Let Map = new Map(); for (let index = 0; index < nums.length; Index++) {// determine if the current value is stored in Map, Target if (typeof map.get(nums[index]) === 'number') {// Return the saved index and the current index [map.get(nums[index]), index]; } // Target = target; // target = target; // target = target; Map. set(target-nums [index], index); }};Copy the code