0015.3 Sum

15. 3Sum #

Problem #

Given an array nums of n integers, are there elements a, b, c in nums 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.

Example:


Given array nums = [-1, 0, 1, 2, -1, -4],

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

Problem Summary #

Given an array, find all combinations of 3 numbers in this array whose sum is 0.

Solution Approach #

Use a map to precompute the sums of any 2 numbers and store them, which can reduce the time complexity to O(n^2). The troublesome part of this problem is that when outputting the final solutions, the output must not contain duplicate solutions. The same number may appear multiple times in the array, and the same number may also be used multiple times, but when outputting the final solutions, there must be no duplicates. For example, [-1,-1,2], [2, -1, -1], and [-1, 2, -1] are 3 duplicate solutions. Even if -1 may appear 100 times, the array indices of the -1 used each time are different.

Here, deduplication and sorting are needed. The map records the number of times each number appears, then the map’s key array is sorted. Finally, scan this sorted array to find combinations where the other 2 numbers can sum with the current number to 0.

Code #


package leetcode

import (
	"sort"
)

// Solution 1: optimal solution, two pointers + sorting
func threeSum(nums []int) [][]int {
	sort.Ints(nums)
	result, start, end, index, addNum, length := make([][]int, 0), 0, 0, 0, 0, len(nums)
	for index = 1; index < length-1; index++ {
		start, end = 0, length-1
		if index > 1 && nums[index] == nums[index-1] {
			start = index - 1
		}
		for start < index && end > index {
			if start > 0 && nums[start] == nums[start-1] {
				start++
				continue
			}
			if end < length-1 && nums[end] == nums[end+1] {
				end--
				continue
			}
			addNum = nums[start] + nums[end] + nums[index]
			if addNum == 0 {
				result = append(result, []int{nums[start], nums[index], nums[end]})
				start++
				end--
			} else if addNum > 0 {
				end--
			} else {
				start++
			}
		}
	}
	return result
}

// Solution 2
func threeSum1(nums []int) [][]int {
	res := [][]int{}
	counter := map[int]int{}
	for _, value := range nums {
		counter[value]++
	}

	uniqNums := []int{}
	for key := range counter {
		uniqNums = append(uniqNums, key)
	}
	sort.Ints(uniqNums)

	for i := 0; i < len(uniqNums); i++ {
		if (uniqNums[i]*3 == 0) && counter[uniqNums[i]] >= 3 {
			res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[i]})
		}
		for j := i + 1; j < len(uniqNums); j++ {
			if (uniqNums[i]*2+uniqNums[j] == 0) && counter[uniqNums[i]] > 1 {
				res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[j]})
			}
			if (uniqNums[j]*2+uniqNums[i] == 0) && counter[uniqNums[j]] > 1 {
				res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[j]})
			}
			c := 0 - uniqNums[i] - uniqNums[j]
			if c > uniqNums[j] && counter[c] > 0 {
				res = append(res, []int{uniqNums[i], uniqNums[j], c})
			}
		}
	}
	return res
}




Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文