# 三数之和

<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>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <algorithm>
#include <set>
class Solution
{
public:
	vector<vector<int>> threeSum(vector<int> &nums)
	{
		vector<vector<int>> r;
		if (nums.size() == 0)
			return r;
		sort(nums.begin(), nums.end());
		int cur, left, right;
		cur = 0;
		while (cur < nums.size())
		{
			if (nums[cur] > 0)
				break;
			left = cur + 1;
			right = nums.size() - 1;
			while (left < right)
			{
				int n = nums[cur] + nums[left] + nums[right];
				if (n == 0)
				{
					r.emplace_back(vector<int>({nums[cur], nums[left], nums[right]}));
					int t = left + 1;
					__________________________;
				}
				else if (n > 0)
				{
					int t = right - 1;
					while (t > left && nums[t] == nums[right])
						t--;
					right = t;
				}
				else
				{
					int t = left + 1;
					while (t < right && nums[t] == nums[left])
						t++;
					left = t;
				}
			}
			int t = cur + 1;
			while (t < nums.size() && nums[t] == nums[cur])
				t++;
			cur = t;
		}
		return r;
	}
};
```

## template

```cpp
#include <algorithm>
#include <set>
class Solution
{
public:
	vector<vector<int>> threeSum(vector<int> &nums)
	{
		vector<vector<int>> r;
		if (nums.size() == 0)
			return r;
		sort(nums.begin(), nums.end());
		int cur, left, right;
		cur = 0;
		while (cur < nums.size())
		{
			if (nums[cur] > 0)
				break;
			left = cur + 1;
			right = nums.size() - 1;
			while (left < right)
			{
				int n = nums[cur] + nums[left] + nums[right];
				if (n == 0)
				{
					r.emplace_back(vector<int>({nums[cur], nums[left], nums[right]}));
					int t = left + 1;
					while (t < right && nums[t] == nums[left])
						t++;
					left = t;
					t = right - 1;
					while (t > left && nums[t] == nums[right])
						t--;
					right = t;
				}
				else if (n > 0)
				{
					int t = right - 1;
					while (t > left && nums[t] == nums[right])
						t--;
					right = t;
				}
				else
				{
					int t = left + 1;
					while (t < right && nums[t] == nums[left])
						t++;
					left = t;
				}
			}
			int t = cur + 1;
			while (t < nums.size() && nums[t] == nums[cur])
				t++;
			cur = t;
		}
		return r;
	}
};
```

## 答案

```cpp
while (t < right && nums[t] == nums[left])
	t++;
left = t;
t = right - 1;
while (t > left && nums[t] == nums[right])
	t--;
right = t;
```

## 选项

### A

```cpp
while (t < right)
	t++;
left = t;
t = right - 1;
while (t > left)
	t--;
right = t;
```

### B

```cpp
while (t < right)
	t++;
left = t;
t = right + 1;
while (t > left)
	t--;
right = t;
```

### C

```cpp
while (t < right && nums[t] == nums[left])
	t++;
left = t;
t = right + 1;
while (t > left && nums[t] == nums[right])
	t--;
right = t;
```