###### 15. 3Sum（求数组中和为0的3个数）

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]


class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
int len = nums.length;
for (int i = 0; i < len; i++) {
int twoSum = 0 - nums[i];
int left = i + 1, right = len - 1;
while (left < right) {
if (nums[left] + nums[right] > twoSum) {
right--;
} else if (nums[left] + nums[right] < twoSum) {
left++;
} else {
left++;
while (left < len && nums[left] == nums[left - 1])
left++;
right--;
while (right >= 0 && nums[right] == nums[right + 1])
right--;
}
}
while (i < len - 1 && nums[i] == nums[i + 1])
i++;
}
return res;
}
}

#### 数组

2017年01月30日 17:30

#### Leetcode解题笔记（Array）

2016-07-23 22:38:02

#### [LeetCode]3Sum,解题报告

2014-06-13 23:42:52

#### Leetcode全求和问题

2014-02-04 10:29:10

#### LeetCode – 4Sum (Java)题解

2016-03-20 17:33:50

#### leetcode（找出三个数数组和接近所给数）

2017-08-19 17:18:09

#### [leetcode: Python]18.4Sum

2017-05-19 08:18:55

#### leetcode--ksum问题--3sum

2016-07-17 22:08:28

#### 【leetcode】3SUM python实现

2016-06-03 13:08:40

#### LeetCode --- 15. 3Sum

2015-01-29 19:38:27

## 不良信息举报

15. 3Sum（求数组中和为0的3个数）