/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
/*
var twoSum = function(nums, target) {
var targetMap = new Map();
var result = new Array();
for(var i = 0; i < nums.length; i++)
{
var diff = target - nums[i];
if(!targetMap.has(diff)){
targetMap.set(nums[i],i);
}else{
result[0] = targetMap.get(diff);
result[1] = i;
return result;
}
}
return result;
};
*/
const twoSum = function(nums, target) {
const comp = {};
for(let i=0; i<nums.length; i++){
if(comp[nums[i] ]>=0){
return [ comp[nums[i] ] , i]
}
comp[target-nums[i]] = i
}
};
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter