Title: #15 - 3Sum - LeetCode JavaScript Solutions
Open Graph Title: #15 - 3Sum - LeetCode JavaScript Solutions
X Title: #15 - 3Sum - LeetCode JavaScript Solutions
Description: Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j]...
Open Graph Description: Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j]...
X Description: Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j]...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/3sum
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/3sum","name":"#15 - 3Sum - LeetCode JavaScript Solutions","description":"Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j]...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[]} nums\n * @return {number[][]}\n */\nvar threeSum = function(nums) {\n const result = [];\n nums.sort((a, b) => a - b);\n\n for (let i = 0; i < nums.length - 2; i++) {\n if (i > 0 && nums[i] === nums[i - 1]) continue;\n let j = i + 1;\n let k = nums.length - 1;\n while (j < k) {\n const sum = nums[i] + nums[j] + nums[k];\n if (!sum) {\n result.push([nums[i], nums[j], nums[k]]);\n j++;\n k--;\n while (j < k && nums[j] === nums[j - 1]) {\n j++;\n }\n while (j < k && nums[k] === nums[k + 1]) {\n k--;\n }\n } else {\n sum < 0 ? j++ : k--;\n }\n }\n }\n\n return result;\n};","keywords":"LeetCode 15, #15 - 3Sum, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/3sum |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/3sum/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0015-3sum.js |
| Array | https://leetcodejavascript.com/tags/array |
| Sorting | https://leetcodejavascript.com/tags/sorting |
| Two Pointers | https://leetcodejavascript.com/tags/two-pointers |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow