Skip to content

Commit 6d6a640

Browse files
committed
two sum solution
1 parent c10802e commit 6d6a640

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

two-sum/prgmr99.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+
var twoSum = function (nums, target) {
7+
const numMap = new Map();
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
const firstNum = nums[i];
11+
const secondNum = target - nums[i];
12+
13+
if (numMap.has(secondNum)) {
14+
return [numMap.get(secondNum), i];
15+
}
16+
17+
numMap.set(firstNum, i);
18+
}
19+
};

0 commit comments

Comments
 (0)