MCPcopy Index your code
hub / github.com/neetcode-gh/leetcode / threeSum

Function threeSum

javascript/0015-3sum.js:5–33  ·  view source on GitHub ↗
(nums)

Source from the content-addressed store, hash-verified

3 * @return {number[][]}
4 */
5var threeSum = function (nums) {
6 const res = [];
7 nums.sort((a, b) => a - b);
8
9 for (let i = 0; i < nums.length; i++) {
10 const a = nums[i];
11 if (a > 0) break;
12 if (i > 0 && a === nums[i - 1]) continue;
13
14 let l = i + 1;
15 let r = nums.length - 1;
16 while (l < r) {
17 const threeSum = a + nums[l] + nums[r];
18 if (threeSum > 0) {
19 r--;
20 } else if (threeSum < 0) {
21 l++;
22 } else {
23 res.push([a, nums[l], nums[r]]);
24 l++;
25 r--;
26 while (nums[l] === nums[l - 1] && l < r) {
27 l++;
28 }
29 }
30 }
31 }
32 return res;
33};

Callers

nothing calls this directly

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected