Skip to content

Commit 12c6c5a

Browse files
committed
add solution: two-sum
1 parent 979169f commit 12c6c5a

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

two-sum/yihyun-kim1.js

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

0 commit comments

Comments
 (0)