题解 | #三数之和#
三数之和
https://www.nowcoder.com/practice/345e2ed5f81d4017bbb8cc6055b0b711
package main import "sort" /** * * @param num int整型一维数组 * @return int整型二维数组 */ func threeSum(num []int) [][]int { sort.Ints(num) n := len(num) res := make([][]int, 0) for i := 0; i < n; { val := 0 - num[i] for j, k := i+1, n-1; j < k; { if num[j]+num[k] < val { j++ } else if num[j]+num[k] > val { k-- } else { res = append(res, []int{num[i], num[j], num[k]}) for curj := j; j < k && num[j] == num[curj]; j++ { } for curk := k; j < k && num[k] == num[curk]; k-- { } } } for curi := i;i < n && num[curi] == num[i];i++{ } } return res // write code here }