forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.js
44 lines (39 loc) · 878 Bytes
/
Solution.js
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
36
37
38
39
40
41
42
43
44
/**
* Author: limbowandering
*/
const twoSum = function (nums, target) {
const map = {};
for (let i = 0; i < nums.length; i++) {
if (map[nums[i]] !== undefined) {
return [map[nums[i]], i];
} else {
map[target - nums[i]] = i;
}
}
};
/**
* Author: Mcnwork2018
*/
var twoSum = function (nums, target) {
let len = nums.length;
let n = {};
for (let i = 0; i < len; i++) {
if (n[target - nums[i]] !== undefined) {
return [n[target - nums[i]], i];
}
n[nums[i]] = i;
}
};
/**
* Author: rookie
*/
var twoSum = function (nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
if (map.has(target - nums[i])) {
return [map.get(target - nums[i]), i];
}
map.set(nums[i], i);
}
return [];
};