# 三数之和

<p>给你一个包含 <code>n</code> 个整数的数组 <code>nums</code>，判断 <code>nums</code> 中是否存在三个元素 <em>a，b，c ，</em>使得 <em>a + b + c = </em>0 ？请你找出所有和为 <code>0</code> 且不重复的三元组。</p><p><strong>注意：</strong>答案中不可以包含重复的三元组。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>nums = [-1,0,1,2,-1,-4]<strong><br />输出：</strong>[[-1,-1,2],[-1,0,1]]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>nums = []<strong><br />输出：</strong>[]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>nums = [0]<strong><br />输出：</strong>[]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>0 <= nums.length <= 3000</code></li>	<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li></ul>

## template

```python
from typing import List
class Solution:
	def threeSum(self, nums: List[int]) -> List[List[int]]:
		if len(nums) == 0:
			return []
		result = []
		unique = {}
		inv = {}
		left = None
		right = None
		nums.sort()
		i = 0
		while i < len(nums):
			if left == None and nums[i] >= 0:
				left = i
			if right == None and nums[i] > 0:
				right = i
			inv[nums[i]] = i
			i += 1
		if left == 0:
			right = len(nums)
		if right is None:
			return []
		i = 0
		while i < right:
			j = i+1
			while j < len(nums) and (-nums[i] >= nums[j] * 2):
				last = 0-nums[i]-nums[j]
				k = inv.get(last)
				if k and k > j:
					list = [nums[i], nums[j], last]
					hash = f'{list[0]}_{list[1]}_{list[2]}'
					if unique.get(hash) is None:
						unique[hash] = True
						result.append(list)
				j += 1
			i += 1
		return result
# %%
s = Solution()
print(s.threeSum(nums = [-1,0,1,2,-1,-4]))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```