-
Notifications
You must be signed in to change notification settings - Fork 0
/
twoSum.js
114 lines (76 loc) · 1.63 KB
/
twoSum.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
let output = []
// for (let index = 0; index < nums.length; index++) {
// for (let number = 0; number < nums.length; number++) {
// if (index === number) {
// continue
// }
// if (target === (nums[index] + nums[number])) {
// output = [number, index]
// }
// }
// }
let hash = {};
for (let i = 0; i < nums.length; i++) {
let complement = target - nums[i]; // 3
// console.log({ hash })
// console.log({ complement })
// console.log({ comp: hash[complement] })
// console.log('------')
// 1 && 1
if (hash[complement] >= 0 && hash[complement] !== i) {
output = [i, hash[complement]];
break;
}
hash[nums[i]] = i;
}
// console.log(twoSum([2, 11, 7, 15], 9));
// for (let i = 0; i < nums.length; i++) {
// let res = target - nums[i];
// if (hash[res]) {
// output = [i, hash[res]];
// break;
// }
// }
return output
};
/*{
0: [2, 11, 7, 15],
1: [2, 11, 7, 15],
2: [2, 11, 7, 15],
3: [2, 11, 7, 15],
}
{
2: 0,
11: 1,
7: 2,
15: 3
}
if ()
let arr = [1,2,3,4];
for a in arr {
}
for a in arr {
}
*/
console.log(twoSum([2, 11, 7, 15], 9));
console.log(twoSum([3, 2, 4], 6));
console.log(twoSum([3, 3], 6));
/*
if num[0] + num[1] === target
return [0,1]
else
if num[0] + num[2] === target
return [0,2]
nums[1 0 2 1 3 4 5] target = 3
i=0; nums i++
(num[i] + nums[i++]) === 3
i = 0; nums; i++
number = 1
nums[2] + nums[3]
*/