-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0016_3Sum_Closest.go
54 lines (49 loc) · 987 Bytes
/
0016_3Sum_Closest.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
package leetcode
import (
"math"
"sort"
)
func threeSumClosest(nums []int, target int) int {
result, different := 0, math.MaxInt16
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
for k := j + 1; k < len(nums); k++ {
if abs(nums[i]+nums[j]+nums[k]-target) < different {
different = abs(nums[i] + nums[j] + nums[k] - target)
result = nums[i] + nums[j] + nums[k]
}
}
}
}
return result
}
func abs(a int) int {
if a > 0 {
return a
} else {
return -a
}
}
func threeSumClosest2(nums []int, target int) int {
n, result, different := len(nums), 0, math.MaxInt16
sort.Ints(nums)
for i := 0; i < n-2; i++ {
left, right := i+1, n-1
for nums[i] == nums[i+1] && i < n-2 {
i++
}
for left < right {
sum := nums[i] + nums[left] + nums[right]
if abs(sum-target) < different {
different = abs(sum - target)
result = sum
}
if sum < target {
left++
} else {
right--
}
}
}
return result
}