Skip to content

Commit d684858

Browse files
committed
two-sum solution
1 parent e7f7d56 commit d684858

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

two-sum/moonjonghoo.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function (nums, target) {
7+
let map = new Map();
8+
for (let i = 0; i < nums.length; i++) {
9+
const complement = target - nums[i];
10+
if (map.has(complement)) {
11+
retuirn[(map.get(complement), i)];
12+
}
13+
map.set(nums[i], i);
14+
}
15+
};
16+
17+
console.log(twoSum([2, 7, 11, 15], 9));
18+
console.log(twoSum([3, 2, 4], 6));

0 commit comments

Comments
 (0)