Skip to content

Commit

Permalink
Update: 0015-3sum.js
Browse files Browse the repository at this point in the history
  • Loading branch information
benmarg committed Jan 24, 2023
1 parent 778fc8c commit b0a44ba
Showing 1 changed file with 28 additions and 46 deletions.
74 changes: 28 additions & 46 deletions javascript/0015-3sum.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,34 @@
/**
* https://leetcode.com/problems/3sum/
* Time O(N ^ 2) | Space O(N)
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums, sums = []) {
nums.sort((a, b) => a - b);

for (let first = 0; first < nums.length - 2; first++) {
if (isPrevDuplicate(nums, first)) continue;

const [target, left, right] = [
-nums[first],
first + 1,
nums.length - 1,
];

search(nums, target, left, right, sums);
}

return sums;
};

const isPrevDuplicate = (nums, index) => nums[index - 1] === nums[index];

const isNextDuplicate = (nums, index) => nums[index] === nums[index + 1];

const search = (nums, target, left, right, sums) => {
while (left < right) {
const [leftVal, rightVal] = [nums[left], nums[right]];
const sum = leftVal + rightVal;

const isTarget = sum === target;
if (isTarget) {
sums.push([-target, leftVal, rightVal]);
left++;
right--;

while (left < right && isPrevDuplicate(nums, left)) left++;
while (left < right && isNextDuplicate(nums, right)) right--;

continue;
var threeSum = function(nums) {
const res = [];
nums.sort((a,b) => a-b)

for (let i = 0; i < nums.length; i++) {
const a = nums[i];
if (a > 0) break;
if (i > 0 && a === nums[i - 1]) continue;

let l = i + 1;
let r = nums.length - 1;
while (l < r) {
const threeSum = a + nums[l] + nums[r];
if (threeSum > 0) {
r--;
} else if (threeSum < 0) {
l++;
} else {
res.push([a, nums[l], nums[r]]);
l++;
r--;
while (nums[l] === nums[l - 1] && l < r) {
l++;
}
}
}

const isTargetGreater = sum < target;
if (isTargetGreater) left++;

const isTargetLess = target < sum;
if (isTargetLess) right--;
}
};
return res;
}

0 comments on commit b0a44ba

Please sign in to comment.