Skip to content

Commit

Permalink
Time: 151 ms (82.69%), Space: 64.3 MB (91.80%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
rldnrl committed Mar 22, 2024
1 parent b482230 commit 6e029fc
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions 0015-3sum/0015-3sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@
* @return {number[][]}
*/
var threeSum = function(nums) {
const N = nums.length
const answer = []
nums.sort((a, b) => a - b);
nums.sort((a, b) => (a - b))

for (let i = 0; i < nums.length; i++) {
for (let i = 0; i < N; i++) {
if (i > 0 && nums[i] === nums[i - 1]) {
continue;
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++;
let left = i + 1
let right = N - 1

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

return answer;
return answer
};

0 comments on commit 6e029fc

Please sign in to comment.