-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
67 lines (64 loc) · 1.53 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
let twoSum = (nums, target) => {
let tmp = {};
for (let [index, num] of nums.entries) {
if (tmp[num] !== undefined) return [tmp[num], index];
tmp[target - num] = index;
}
}
// const twoSum = (nums, target) => {
// console.log("nums: " + nums);
// console.log("entries: ");
// console.log(nums.entries());
// let tmp = {};
//
// for (let [i, n] of nums.entries()) {
// console.log("[num, i]");
// console.log([n, i]);
//
// // curr idx
// let ci = tmp[n];
// // console.log(`${item} -- ${i}`)
// console.log("ci:")
// console.log(ci)
//
// let exp = target - n;
// console.log("nv:")
// console.log(exp)
//
// if (ci !== undefined) {
// console.log("Found")
// return [ci, i];
// } else {
// tmp[exp] = i;
// }
// }
//
// /**
// * In your twoSum function,
// * the forEach loop is not stopping because return inside a forEach loop does not stop the loop.
// * It only stops the current iteration and moves on to the next iteration.
// * To stop the loop early, you can use a regular for loop instead of forEach.
// */
// // nums.forEach(function (item, i) {
// // // curr idx
// // let ci = tmp[item];
// // let nv = target - item;
// //
// // if (ci !== undefined) {
// // return [ci, i];
// // }
// //
// // tmp[target - item] = i;
// // })
// }
console.log(twoSum([2, 7, 11, 15], 9));
/**
* [2, 7]
*
*
*/