-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththreesum.go
38 lines (35 loc) · 849 Bytes
/
threesum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package array
import "sort"
// https://leetcode-cn.com/problems/3sum/
// https://leetcode-cn.com/problems/3sum/solution/san-shu-zhi-he-by-leetcode-solution/
// Time: O(n^2 + nlogn) = O(n^2)
func threeSum(nums []int) [][]int {
sort.Ints(nums)
n := len(nums)
var ret [][]int
// first loop find the element-1
for i := 0; i < n; i++ {
// first element or next different element
if i == 0 || nums[i] != nums[i-1] {
k := n - 1
// second loop find element-2
for j := i + 1; j < n; j++ {
if j == i+1 || nums[j] != nums[j-1] {
for k > j && nums[i]+nums[j]+nums[k] > 0 {
k--
}
if k > j {
ok := checkZero(nums[i], nums[j], nums[k])
if ok {
ret = append(ret, []int{nums[i], nums[j], nums[k]})
}
}
}
}
}
}
return ret
}
func checkZero(a, b, c int) bool {
return a+b+c == 0
}