-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0015_3sum.go
More file actions
48 lines (40 loc) · 857 Bytes
/
s0015_3sum.go
File metadata and controls
48 lines (40 loc) · 857 Bytes
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
39
40
41
42
43
44
45
46
47
48
/*
https://leetcode.com/problems/3sum/
Given an integer array nums, return all the triplets [nums[i], nums[j],
nums[k]]
such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
*/
package solutions
import "sort"
func threeSum(nums []int) [][]int {
res := &[][]int{}
sort.Ints(nums)
for i, n := range nums {
if n > 0 {
break
}
if i == 0 || nums[i-1] != nums[i] {
twoSumII(nums, i, res)
}
}
return *res
}
func twoSumII(nums []int, i int, res *[][]int) {
lo, hi := i+1, len(nums)-1
for lo < hi {
sum := nums[i] + nums[lo] + nums[hi]
if sum < 0 {
lo++
} else if sum > 0 {
hi--
} else {
*res = append(*res, []int{nums[i], nums[lo], nums[hi]})
lo++
hi--
for lo < hi && nums[lo] == nums[lo-1] {
lo++
}
}
}
}