Skip to content

Commit 7150355

Browse files
committed
Hash map O(n) O(n-1)
1 parent 78cb3d6 commit 7150355

File tree

1 file changed

+82
-62
lines changed

1 file changed

+82
-62
lines changed

leetcode-1-TwoSum.js

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,102 @@
1-
// // https://leetcode.com/problems/two-sum/
1+
// // // https://leetcode.com/problems/two-sum/
22

3-
// p: arr, num
4-
// r: arr
3+
// p: array, num (int)
4+
// r: array
5+
// n
6+
// e: nums = [2,7,3,1,6], 8 => [0, 4]
7+
// 0 1 2 3 4
8+
// map 6 1 5 7
9+
// o
510
//
6-
// e: [2,7,11,15], 9
7-
// 2: 0
8-
// 7:
9-
10-
// [3,2,4], 6
11-
// 3: 0
12-
// 2: 1
13-
// 4:
14-
15-
// [3,3], 6
16-
// 3: 0
17-
// 3:
1811
//
1912

2013
var twoSum = function (nums, target) {
2114
const map = {};
2215
for (let i = 0; i < nums.length; i++) {
23-
let num1 = nums[i];
24-
let num2 = target - num1;
25-
26-
if (num2 in map && map[num2] !== i) return [map[num2], i];
27-
map[num1] = i;
16+
let num = nums[i];
17+
let oNum = target - num;
18+
if (num in map) return [map[num], i];
19+
map[oNum] = i;
2820
}
2921
};
3022

31-
// // p: arr of ints, int
32-
// // r: arr int+
33-
// // e:
34-
// // Example 1:
23+
// // p: arr, num
24+
// // r: arr
25+
// //
26+
// // e: [2,7,11,15], 9
27+
// // 2: 0
28+
// // 7:
29+
30+
// // [3,2,4], 6
31+
// // 3: 0
32+
// // 2: 1
33+
// // 4:
34+
35+
// // [3,3], 6
36+
// // 3: 0
37+
// // 3:
38+
// //
39+
40+
// var twoSum = function (nums, target) {
41+
// const map = {};
42+
// for (let i = 0; i < nums.length; i++) {
43+
// let num1 = nums[i];
44+
// let num2 = target - num1;
45+
46+
// if (num2 in map && map[num2] !== i) return [map[num2], i];
47+
// map[num1] = i;
48+
// }
49+
// };
50+
51+
// // // p: arr of ints, int
52+
// // // r: arr int+
53+
// // // e:
54+
// // // Example 1:
55+
56+
// // // Input: nums = [2,7,11,15], target = 9
57+
// // // Output: [0,1]
58+
// // // Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
59+
// // // Example 2:
60+
61+
// // // Input: nums = [3,2,4], target = 6
62+
// // // Output: [1,2]
63+
// // // Example 3:
3564

36-
// // Input: nums = [2,7,11,15], target = 9
37-
// // Output: [0,1]
38-
// // Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
39-
// // Example 2:
65+
// // // Input: nums = [3,3], target = 6
66+
// // // Output: [0,1]
4067

41-
// // Input: nums = [3,2,4], target = 6
42-
// // Output: [1,2]
43-
// // Example 3:
68+
// // // i: [3,0,2,6] ; target = 6
69+
// // // o: [1,3]
4470

45-
// // Input: nums = [3,3], target = 6
46-
// // Output: [0,1]
71+
// // // var twoSum = function (nums, target) {
72+
// // // for (let i = 0; i < nums.length; i++) {
73+
// // // const numsClone = [...nums];
74+
// // // numsClone.splice(i, 1, "null");
75+
// // // // console.log(numsClone);
76+
// // // for (let j = 0; j < numsClone.length; j++) {
77+
// // // if (nums[i] + numsClone[j] === target) {
78+
// // // console.log(i, j);
79+
// // // return [i, j];
80+
// // // }
81+
// // // }
82+
// // // }
83+
// // // };
4784

48-
// // i: [3,0,2,6] ; target = 6
49-
// // o: [1,3]
85+
// // // better solution:
86+
// // const twoSum = (nums, target) => {
87+
// // const map = {};
5088

51-
// // var twoSum = function (nums, target) {
5289
// // for (let i = 0; i < nums.length; i++) {
53-
// // const numsClone = [...nums];
54-
// // numsClone.splice(i, 1, "null");
55-
// // // console.log(numsClone);
56-
// // for (let j = 0; j < numsClone.length; j++) {
57-
// // if (nums[i] + numsClone[j] === target) {
58-
// // console.log(i, j);
59-
// // return [i, j];
60-
// // }
90+
// // let oppositeNum = target - nums[i]; // [2, 11, 15, 7] => [7, -2, -6, 2]
91+
// // if (oppositeNum in map) {
92+
// // console.log([map[oppositeNum], i]);
93+
// // return [map[oppositeNum], i];
6194
// // }
95+
// // map[nums[i]] = i;
6296
// // }
6397
// // };
6498

65-
// // better solution:
66-
// const twoSum = (nums, target) => {
67-
// const map = {};
68-
69-
// for (let i = 0; i < nums.length; i++) {
70-
// let oppositeNum = target - nums[i]; // [2, 11, 15, 7] => [7, -2, -6, 2]
71-
// if (oppositeNum in map) {
72-
// console.log([map[oppositeNum], i]);
73-
// return [map[oppositeNum], i];
74-
// }
75-
// map[nums[i]] = i;
76-
// }
77-
// };
78-
79-
// twoSum([2, 11, 15, 7], 9);
80-
// // twoSum([1, 0, 2, 6], 6);
81-
// // twoSum([3, 3], 6);
82-
// // twoSum([0, 4, 3, 0], 0);
99+
// // twoSum([2, 11, 15, 7], 9);
100+
// // // twoSum([1, 0, 2, 6], 6);
101+
// // // twoSum([3, 3], 6);
102+
// // // twoSum([0, 4, 3, 0], 0);

0 commit comments

Comments
 (0)