> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# 3Sum Python Solution with Tests

> Tested Python solution for LeetCode 15 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 15, Medium. Topics: Array, Two Pointers, Sorting. [View on LeetCode](https://leetcode.com/problems/three-sum/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 15   # by problem number
lcpy gen -s three_sum   # by problem name
```

## Problem

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] + nums[k] == 0`.

Notice that the solution set must not contain duplicate triplets.

### Examples

```
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
```

**Explanation:**
nums\[0] + nums\[1] + nums\[2] = (-1) + 0 + 1 = 0.
nums\[1] + nums\[2] + nums\[4] = 0 + 1 + (-1) = 0.
nums\[0] + nums\[3] + nums\[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are \[-1,0,1] and \[-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.

```
Input: nums = [0,1,1]
Output: []
```

**Explanation:** The only possible triplet does not sum up to 0.

```
Input: nums = [0,0,0]
Output: [[0,0,0]]
```

**Explanation:** The only possible triplet sums up to 0.

### Constraints

* 3 \<= nums.length \<= 3000
* -10^5 \<= nums\[i] \<= 10^5

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/three_sum/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/three_sum/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2)
    # Space: O(k) where k is number of unique triplets
    def three_sum(self, nums: list[int]) -> list[list[int]]:
        nums.sort()
        result = set()

        for i in range(len(nums) - 2):
            left, right = i + 1, len(nums) - 1

            while left < right:
                total = nums[i] + nums[left] + nums[right]

                if total < 0:
                    left += 1
                elif total > 0:
                    right -= 1
                else:
                    result.add((nums[i], nums[left], nums[right]))
                    left += 1
                    right -= 1

        return [list(triplet) for triplet in result]
```

## Complexity

| Time   | Space                                     |
| ------ | ----------------------------------------- |
| O(n^2) | O(k) where k is number of unique triplets |

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
