Last active
July 17, 2021 07:04
-
-
Save ravitejamuddada/0cd155f2ec464d15428ccbbf4cb537f9 to your computer and use it in GitHub Desktop.
leetcode two sum
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
/** | |
Input: nums = [2,7,11,15], target = 9 | |
Output: [0,1] | |
Output: Because nums[0] + nums[1] == 9, we return [0, 1]. | |
*/ | |
/** | |
* @param {number[]} nums | |
* @param {number} target | |
* @return {number[]} | |
*/ | |
var twoSum = function(nums, target) { | |
let result=[]; | |
const map=new Map(); | |
for(let i in nums){ | |
if(map.has(nums[i])){ | |
result[0]=i; | |
result[1]=map.get(nums[i]); | |
return result; | |
} | |
else{ | |
map.set(target-nums[i],i); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment