two sum
From 21:10 - 21:30array
map
My solution:
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
var res = [];
// value : index
var map = {};
// tranfer arr to map
// attrieve complexity n -> 1
for(var i=0; i
- Other submittion
var twoSum = function(nums, target) {
for(let i=0;i
var twoSum = function(nums, target) {
const map = {};
for(let i = 0; i < nums.length; i++) {
const currentValue = nums[i];
const wantedValue = target - currentValue;
if(map[wantedValue] !== undefined) {
return [map[wantedValue], i];
}
map[currentValue] = i;
}
};