-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_two_sum.js
More file actions
35 lines (29 loc) · 948 Bytes
/
Copy path1_two_sum.js
File metadata and controls
35 lines (29 loc) · 948 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
29
30
31
32
33
34
35
// tags: #hashmap
// https://leetcode.cn/problems/two-sum/
// version 1
// time: 64ms
var twoSum = function (nums, target) {
const map = new Map();
nums.forEach((num, idx) => map.set(num, idx));
for (let i = 0; i < nums.length; i++) {
const pair = map.get(target - nums[i]);
if (pair && pair !== i) return [i, pair];
}
};
// version 2
// time 60ms | beat 93%
// 只循环一次且 map 不需要塞满
var twoSum = function (nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const pair = map.get(target - nums[i]);
// note here pair could be 0, so can't use if (pair)
if (pair !== undefined) return [pair, i];
map.set(nums[i], i);
}
};
/* test code */
const assert = require("node:assert/strict");
// 同一个位置元素不能使用两次,但不同位置同值元素没问题
assert.deepEqual(twoSum([3, 3], 6), [0, 1]);
assert.deepEqual(twoSum([1, 3, 4, 2], 6), [2, 3]);