Skip to content

Commit

Permalink
Time: 139 ms (89.70%), Space: 53 MB (44.63%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
rldnrl committed Jan 1, 2023
1 parent 47b12e5 commit 1383204
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 0015-3sum/0015-3sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
const answer = []
nums.sort((a, b) => a - b);

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

let left = i + 1;
let right = nums.length - 1;
while (left < right) {
let sum = nums[i] + nums[left] + nums[right];
if (sum > 0) {
right--;
} else if (sum < 0) {
left++;
} else {
answer.push([nums[i], nums[left], nums[right]])
left++;
while (nums[left] === nums[left - 1] && left < right) {
left++;
}
}
}
}

return answer;
};

0 comments on commit 1383204

Please sign in to comment.