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.
Example 1:
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.
Example 2:
Input: nums = [0,1,1] Output: [] Explanation: The only possible triplet does not sum up to 0.
Example 3:
Input: nums = [0,0,0] Output: [[0,0,0]] Explanation: The only possible triplet sums up to 0.
Constraints:
3 <= nums.length <= 3000
-105 <= nums[i] <= 105
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
n, res = len(nums), []
if n < 3:
return res
nums.sort()
for i in range(n - 2):
if nums[i] > 0:
break
if i > 0 and nums[i] == nums[i - 1]:
continue
j, k = i + 1, n - 1
while j < k:
if nums[i] + nums[j] + nums[k] == 0:
res.append([nums[i], nums[j], nums[k]])
j += 1
k -= 1
while j < n and nums[j] == nums[j - 1]:
j += 1
while k > i and nums[k] == nums[k + 1]:
k -= 1
elif nums[i] + nums[j] + nums[k] < 0:
j += 1
else:
k -= 1
return res
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
if (n < 3) {
return Collections.emptyList();
}
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n - 2 && nums[i] <= 0; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int j = i + 1, k = n - 1;
while (j < k) {
if (nums[i] + nums[j] + nums[k] == 0) {
res.add(Arrays.asList(nums[i], nums[j], nums[k]));
++j;
--k;
while (j < n && nums[j] == nums[j - 1]) {
++j;
}
while (k > i && nums[k] == nums[k + 1]) {
--k;
}
} else if (nums[i] + nums[j] + nums[k] < 0) {
++j;
} else {
--k;
}
}
}
return res;
}
}
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n = nums.size();
if (n < 3) {
return {};
}
sort(nums.begin(), nums.end());
vector<vector<int>> res;
for (int i = 0; i < n - 2 && nums[i] <= 0; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
int j = i + 1, k = n - 1;
while (j < k) {
if (nums[i] + nums[j] + nums[k] == 0) {
res.push_back({nums[i], nums[j], nums[k]});
++j;
--k;
while (j < n && nums[j] == nums[j - 1]) ++j;
while (k > i && nums[k] == nums[k + 1]) --k;
} else if (nums[i] + nums[j] + nums[k] < 0) {
++j;
} else {
--k;
}
}
}
return res;
}
};
func threeSum(nums []int) [][]int {
n, res := len(nums), make([][]int, 0)
if n < 3 {
return res
}
sort.Ints(nums)
for i := 0; i < n-2 && nums[i] <= 0; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
j, k := i+1, n-1
for j < k {
if nums[i]+nums[j]+nums[k] == 0 {
res = append(res, []int{nums[i], nums[j], nums[k]})
j++
k--
for j < n && nums[j] == nums[j-1] {
j++
}
for k > i && nums[k] == nums[k+1] {
k--
}
} else if nums[i]+nums[j]+nums[k] < 0 {
j++
} else {
k--
}
}
}
return res
}
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
const n = nums.length;
if (n < 3) return [];
let res = [];
nums.sort((a, b) => a - b);
for (let i = 0; i < n - 2 && nums[i] <= 0; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
let j = i + 1;
let k = n - 1;
while (j < k) {
if (nums[i] + nums[j] + nums[k] === 0) {
res.push([nums[i], nums[j], nums[k]]);
++j;
--k;
while (nums[j] === nums[j - 1]) ++j;
while (nums[k] === nums[k + 1]) --k;
} else if (nums[i] + nums[j] + nums[k] < 0) {
++j;
} else {
--k;
}
}
}
return res;
};
public class ThreeSumComparer: IEqualityComparer<IList<int>>
{
public bool Equals(IList<int> left, IList<int> right)
{
return left[0] == right[0] && left[1] == right[1] && left[2] == right[2];
}
public int GetHashCode(IList<int> obj)
{
return (obj[0] ^ obj[1] ^ obj[2]).GetHashCode();
}
}
public class Solution {
public IList<IList<int>> ThreeSum(int[] nums) {
Array.Sort(nums);
var results = new HashSet<IList<int>>(new ThreeSumComparer());
var cIndex = Array.BinarySearch(nums, 0);
if (cIndex < 0) cIndex = ~cIndex;
while (cIndex < nums.Length)
{
var c = nums[cIndex];
var aIndex = 0;
var bIndex = cIndex - 1;
while (aIndex < bIndex)
{
if (nums[aIndex] + nums[bIndex] + c < 0)
{
var step = 1;
while (aIndex + step < bIndex && nums[aIndex + step] + nums[bIndex] + c < 0)
{
aIndex += step;
step *= 2;
}
step /= 2;
while (step > 0)
{
if (aIndex + step < bIndex && nums[aIndex + step] + nums[bIndex] + c < 0)
{
aIndex += step;
}
step /= 2;
}
}
if (nums[aIndex] + nums[bIndex] + c > 0)
{
var step = 1;
while (aIndex < bIndex - step && nums[aIndex] + nums[bIndex - step] + c > 0)
{
bIndex -= step;
step *= 2;
}
step /= 2;
while (step > 0)
{
if (aIndex < bIndex - step && nums[aIndex] + nums[bIndex - step] + c > 0)
{
bIndex -= step;
}
step /= 2;
}
}
if (nums[aIndex] + nums[bIndex] + c == 0)
{
var list = new List<int> { nums[aIndex], nums[bIndex], c };
results.Add(list);
++aIndex;
--bIndex;
}
else if (nums[aIndex] + nums[bIndex] + c < 0)
{
++aIndex;
}
else
{
--bIndex;
}
}
++cIndex;
}
return results.ToList();
}
}
# @param {Integer[]} nums
# @return {Integer[][]}
def three_sum(nums)
res = []
nums.sort!
for i in 0..(nums.length - 3)
next if i > 0 && nums[i - 1] == nums[i]
j = i + 1
k = nums.length - 1
while j < k do
sum = nums[i] + nums[j] + nums[k]
if sum < 0
j += 1
elsif sum > 0
k -= 1
else
res += [[nums[i], nums[j], nums[k]]]
j += 1
k -= 1
j += 1 while nums[j] == nums[j - 1]
k -= 1 while nums[k] == nums[k + 1]
end
end
end
res
end
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
const res = [];
const n = nums.length;
for (let i = 0; i < n - 2; i++) {
if (nums[i] > 0) {
break;
}
const target = 0 - nums[i];
let l = i + 1;
let r = n - 1;
while (l < r) {
if (nums[l] + nums[r] === target) {
res.push([nums[i], nums[l], nums[r]]);
l++;
r--;
while (nums[l] === nums[l - 1]) {
l++;
}
while (nums[r] === nums[r + 1]) {
r--;
}
} else if (nums[l] + nums[r] < target) {
l++;
} else {
r--;
}
}
while (nums[i] === nums[i + 1]) {
i++;
}
}
return res;
}
use std::cmp::Ordering;
impl Solution {
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
nums.sort();
let n = nums.len();
let mut res = vec![];
if n < 3 {
return res;
}
let mut i = 0;
while i < n - 2 && nums[i] <= 0 {
let mut l = i + 1;
let mut r = n - 1;
while l < r {
match (nums[i] + nums[l] + nums[r]).cmp(&0) {
Ordering::Less => l += 1,
Ordering::Greater => r -= 1,
Ordering::Equal => {
res.push(vec![nums[i], nums[l], nums[r]]);
l += 1;
r -= 1;
while l < n && nums[l] == nums[l - 1] {
l += 1;
}
while r > 0 && nums[r] == nums[r + 1] {
r -= 1;
}
}
}
}
i += 1;
while i < n - 2 && nums[i] == nums[i - 1] {
i += 1;
}
}
res
}
}