-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-two-sum.js
More file actions
28 lines (24 loc) · 753 Bytes
/
Copy path1-two-sum.js
File metadata and controls
28 lines (24 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var twoSum = function(nums, target) {
const map = nums.reduce((acc, num, idx) => {
acc[num] = idx;
return acc;
}, {})
for(let i = 0; i < nums.length; i++) {
const reminder = target - nums[i];
if (map[reminder] !== undefined && map[reminder] !== i)
return [i, map[reminder]];
}
return [];
};
const data = [
{ nums: [2,7,11,15], target: 9, output: [0,1] },
{ nums: [3,2,4], target: 6, output: [1,2] },
{ nums: [3,3], target: 6, output: [0,1] },
];
for (let d of data) {
console.log(JSON.stringify(d));
const result = twoSum(d.nums, d.target);
console.log('result = ', result);
(JSON.stringify(result) === JSON.stringify(d.output)) ? console.log('ok') : console.error('nok');
console.log('----------');
}