22 lines
565 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
*https://leetcode.cn/problems/two-sum/?envType=study-plan-v2&envId=top-interview-150
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const twoSum = function (nums, target) {
};
/*
创建一个mapmap的key为target - num, value为下标遍历nums如果当前num在map中存在就返回[map.get(num), i]i表示当前
下标
*/
function f1(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
if (map.has(nums[i])) return [map.get(nums[i]), i];
map.set(target - nums[i], i);
}
}