Created
April 16, 2024 00:36
-
-
Save yevmoroz/167c73b5c77175b1643e746e94057353 to your computer and use it in GitHub Desktop.
leetcode sum of 2 from list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @param {number} target | |
* @return {number[]} | |
*/ | |
function twoSum(nums, target) { | |
let mp = new Map() | |
for (let i = 0; i < nums.length; i++) { | |
let diff = target - nums[i] | |
if (mp.has(diff)) { | |
return [i, mp.get(diff)] | |
} | |
mp.set(nums[i], i) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment