-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
97 lines (88 loc) · 1.46 KB
/
run.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package threesum
import (
"fmt"
"sort"
)
// 穷举无法通过时间限制
func threeSum1(nums []int) [][]int {
var data [][]int
if len(nums) < 3 {
return data
}
b := true
for _, v := range nums {
if v != 0 {
b = false
}
}
if b {
data = append(data, []int{0, 0, 0})
return data
}
cm := make(map[string]bool)
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
for k := j + 1; k < len(nums); k++ {
if nums[i]+nums[j]+nums[k] == 0 {
if _, ok := cm[min(nums[i], nums[j], nums[k])]; ok {
continue
}
cm[min(nums[i], nums[j], nums[k])] = true
data = append(data, []int{nums[i], nums[j], nums[k]})
}
}
}
}
return data
}
func min(i, j, k int) string {
var t int
if i > j {
t, i = i, j
j = t
}
if i > k {
t, i = i, k
k = t
}
if j > k {
t, j = j, k
k = t
}
return fmt.Sprintf("%d%d%d", i, j, k)
}
func threeSum(nums []int) [][]int {
var data [][]int
ln := len(nums)
if ln < 3 {
return data
}
target := 0
sort.Ints(nums)
for i := 0; i < ln-2; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
l, r := i+1, ln-1
for l < r {
sum := nums[i] + nums[l] + nums[r]
if sum < target {
l++
} else if sum > target {
r--
} else {
tmp := []int{nums[i], nums[l], nums[r]}
data = append(data, tmp)
l++
r--
for l < r && nums[l] == nums[l-1] {
l++
}
for l < r && nums[r] == nums[r+1] {
r--
}
}
}
}
return data
}