-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3sum.go
73 lines (60 loc) · 1.63 KB
/
3sum.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"sort"
)
// source: https://leetcode.com/problems/3sum/
// I guess this solution is pretty simple and no description is needed
// We could have used the same approach we used for two-sum problem, but
// in this case we need to also dedup result, and it may take too much time to check
// whether triplet is already presented in the result or not
func threeSum(nums []int) [][]int {
if len(nums) < 3 {
return [][]int{}
}
length := len(nums)
res := make([][]int, 0, length/2)
sort.Ints(nums)
for i := 0; i < length; i++ {
if i != 0 && nums[i-1] == nums[i] {
continue
}
for l, r := i+1, length-1; l < r; {
n := nums[i] + nums[l] + nums[r]
if n == 0 {
res = append(res, []int{nums[i], nums[l], nums[r]})
for dup := nums[l]; l < r && nums[l] == dup; {
l++
}
} else if n > 0 {
r--
} else {
l++
}
}
}
return res
}
func main() {
// Example 1
var nums1 = []int{-1, 0, 1, 2, -1, -4}
fmt.Println("Expected: [[-1,-1,2],[-1,0,1]] Output: ", threeSum(nums1))
// Example 2
var nums2 = []int{-2, -2, 4, 4}
fmt.Println("Expected: [[-2,-2,4],[-2,-2,4]] Output: ", threeSum(nums2))
// Example 3
var nums3 = []int{0, 1, 2}
fmt.Println("Expected: [] Output: ", threeSum(nums3))
// Example 4
var nums4 = []int{}
fmt.Println("Expected: [] Output: ", threeSum(nums4))
// Example 5
var nums5 = []int{0}
fmt.Println("Expected: [] Output: ", threeSum(nums5))
// Example 6
var nums6 = []int{0, 1, -1}
fmt.Println("Expected: [-1, 0, 1] Output: ", threeSum(nums6))
// Example 7
var nums7 = []int{0, 1, -1, -1}
fmt.Println("Expected: [-1, 0, 1] Output: ", threeSum(nums7))
}