-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15. 3Sum.js
101 lines (75 loc) · 1.77 KB
/
15. 3Sum.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
/* https://leetcode.com/problems/3sum/ */
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
nums.sort((a, b) => a - b);
for (let i = 1; i < nums.length; i++) {
while
}
};
// var threeSum = function(nums) {
// let ans = [];
// let dict = {};
// let temp;
// let i, j, k;
// nums.sort((a, b) => a - b);
// let moveForward = index => {
// let tempIndex = index - 1;
// while (nums[tempIndex] === nums[index] && index < nums.length) {
// index++;
// }
// return index;
// };
// for (i = 0; i < nums.length; i++) {
// temp = nums[i];
// dict[temp] = dict[temp] || -1;
// dict[temp] = Math.max(i, dict[temp]);
// }
// i = 0;
// while (i < nums.length) {
// j = i + 1;
// while (j < nums.length) {
// temp = -(nums[i] + nums[j]);
// if (dict[temp] !== undefined && j < dict[temp]) {
// ans.push([nums[i], nums[j], nums[dict[temp]]]);
// }
// j = moveForward(j + 1);
// }
// i = moveForward(i + 1);
// }
// return ans;
// };
// var threeSum = function(nums) {
// let ans = [];
// let temp;
// let i, j, k;
// nums.sort((a, b) => a - b);
// let moveForward = index => {
// let tempIndex = index - 1;
// while (nums[tempIndex] === nums[index] && index < nums.length) {
// index++;
// }
// return index;
// };
// i = 0;
// while (i < nums.length) {
// j = i + 1;
// while (j < nums.length) {
// temp = nums[i] + nums[j];
// k = j + 1;
// while (k < nums.length) {
// if (nums[k] + temp === 0) {
// ans.push([nums[i], nums[j], nums[k]]);
// }
// k = moveForward(k + 1);
// }
// j = moveForward(j + 1);
// }
// i = moveForward(i + 1);
// }
// return ans;
// };
console.log(threeSum([-1, 0, 1, 2, -1, -4]));
console.log(threeSum([0, 0, 0, 0]));