先进行排序
然后写个从两端向中间处理的方法
最后循环调用这个方法
func threeSum(nums []int) [][]int {
//先排序
sort.Ints(nums)
res := make([][]int, 0)
f := func(nums []int, begin int, end int, target int) {
for begin < end { //从两端向中间遍历
if nums[begin]+nums[end]+target == 0 {
r := make([]int, 0)
r = append(r, nums[begin], nums[end], target)
res = append(res, r)
//遇到相等的,就快进
for begin < end && nums[begin] == nums[begin+1] {
begin++
}
for begin < end && nums[end] == nums[end-1] {
end--
}
begin++
end--
} else if (target + nums[begin] + nums[end]) < 0 {
begin++
} else {
end--
}
}
}
l := len(nums)
for i := 0; i < l-2; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
f(nums, i+1, l-1, nums[i])
}
return res
}
网友评论