Skip to content

Commit 066d5ec

Browse files
author
이호찬
committed
two-sum solution
1 parent 61578ec commit 066d5ec

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

two-sum/lhc0506.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+
const numsMap = {};
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
const needNum = target - nums[i];
11+
12+
if (needNum in numsMap) {
13+
return [i, numsMap[needNum]];
14+
}
15+
16+
numsMap[nums[i]] = i;
17+
}
18+
};

0 commit comments

Comments
 (0)