Skip to content

Commit 1c06aee

Browse files
committed
two sums solution
1 parent cdbd856 commit 1c06aee

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

two-sum/froggy1014.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 idx = 0; idx < nums.length; idx++) {
9+
const rest = target - nums[idx];
10+
if (map.has(rest)) {
11+
return [map.get(rest), idx];
12+
}
13+
map.set(nums[idx], idx);
14+
}
15+
return [];
16+
};
17+
18+
console.log(twoSum([2, 11, 15, 7], 9));

0 commit comments

Comments
 (0)