|
| 1 | +<h2><a href="https://leetcode.com/problems/3sum/">15. 3Sum</a></h2><h3>Medium</h3><hr><div><p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p> |
| 2 | + |
| 3 | +<p>Notice that the solution set must not contain duplicate triplets.</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong>Example 1:</strong></p> |
| 7 | + |
| 8 | +<pre><strong>Input:</strong> nums = [-1,0,1,2,-1,-4] |
| 9 | +<strong>Output:</strong> [[-1,-1,2],[-1,0,1]] |
| 10 | +<strong>Explanation:</strong> |
| 11 | +nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. |
| 12 | +nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. |
| 13 | +nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. |
| 14 | +The distinct triplets are [-1,0,1] and [-1,-1,2]. |
| 15 | +Notice that the order of the output and the order of the triplets does not matter. |
| 16 | +</pre> |
| 17 | + |
| 18 | +<p><strong>Example 2:</strong></p> |
| 19 | + |
| 20 | +<pre><strong>Input:</strong> nums = [0,1,1] |
| 21 | +<strong>Output:</strong> [] |
| 22 | +<strong>Explanation:</strong> The only possible triplet does not sum up to 0. |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong>Example 3:</strong></p> |
| 26 | + |
| 27 | +<pre><strong>Input:</strong> nums = [0,0,0] |
| 28 | +<strong>Output:</strong> [[0,0,0]] |
| 29 | +<strong>Explanation:</strong> The only possible triplet sums up to 0. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>3 <= nums.length <= 3000</code></li> |
| 37 | + <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li> |
| 38 | +</ul> |
| 39 | +</div> |
0 commit comments