# 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;
}
}

举报原因： 您举报文章：15. 3Sum（求数组中和为0的3个数） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)