解题的关键在于去除重复的组合
func threeSum(nums []int) [][]int {
n := len(nums)
sort.Ints(nums)
ans := make([][]int, 0)
for first := 0; first < n; first++ {
if first > 0 && nums[first] == nums[first-1] {
continue
}
third := n - 1
target := -1 * nums[first]
// 这里的去重指的是试过不能的如果后一个重复的话就不用继续试了,直接跳过
for second := first + 1; second < n; second++ {
if second > first+1 && nums[second] == nums[second-1] {
continue
}
for second < third && nums[second]+nums[third] > target {
third--
}
if second == third {
break
}
if nums[second]+nums[third] == target {
ans = append(ans, []int{nums[first], nums[second], nums[third]})
}
}
}
return ans
}