Skip to content

Commit 403151c

Browse files
committed
improve leetcode 1
1 parent 0bc0476 commit 403151c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

leetcode_1_two_sum_easy_array.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,23 @@ var twoSum = function(nums, target) {
2929
return indices;
3030
};
3131

32+
3233
console.log(twoSum([2, 7, 11, 15], 9));
3334

35+
/**
36+
* @param {number[]} nums
37+
* @param {number} target
38+
* @return {number[]}
39+
*/
40+
var twoSum1 = function(nums, target) {
41+
let indices;
42+
const map = new Map;
43+
nums.map((n, i) => {
44+
const complement = target - n;
45+
if (map.has(complement)) indices = [map.get(complement), i];
46+
map.set(n, i);
47+
});
48+
return indices;
49+
};
50+
51+
console.log(twoSum1([2, 7, 11, 15], 13));

0 commit comments

Comments
 (0)