-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.bk.js
More file actions
30 lines (25 loc) · 757 Bytes
/
Copy path3Sum.bk.js
File metadata and controls
30 lines (25 loc) · 757 Bytes
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
// https://leetcode-cn.com/problems/3sum/
var Test = require('./Common/Test');
var threeSum = function (nums) {
const result = [];
for (let i = 0; i < nums.length - 2; i++) {
// const map = new Map();
const set = new Set();
for (let j = i + 1; j < nums.length; j++) {
const num = nums[j];
const other = - nums[i] - num;
// if (map.has(other)) {
if (set.has(other)) {
// result.push([nums[i], map.get(other), j]);
result.push([nums[i], other, num]);
}
// map.set(num, j);
set.add(num);
}
}
return result;
};
function run(nums) {
Test.run(threeSum, nums);
}
run([-1, 0, 1, 2, -1, -4]);