-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16-3SumClosest.go
More file actions
114 lines (102 loc) · 3.47 KB
/
16-3SumClosest.go
File metadata and controls
114 lines (102 loc) · 3.47 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
// 16. 3Sum Closest
// Given an array nums of n integers and an integer target,
// find three integers in nums such that the sum is closest to target.
// Return the sum of the three integers. You may assume that each input would have exactly one solution.
// Example 1:
// Input: nums = [-1,2,1,-4], target = 1
// Output: 2
// Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
// Example 2:
// Input: nums = [0,0,0], target = 1
// Output: 0
// 解题思路:
// 最优解 O(n^2)
import "fmt"
import "sort"
// 解法一 O(n^2)
func threeSumClosest(nums []int, target int) int {
n, res, diff := len(nums), 0, 1 << 31
abs := func(x int) int { if x < 0 { return -x; }; return x; }
if n > 2 {
sort.Ints(nums)
for i := 0; i < n-2; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
for j, k := i+1, n-1; j < k; {
sum := nums[i] + nums[j] + nums[k]
if abs(sum-target) < diff {
res, diff = sum, abs(sum-target)
}
if sum == target {
return res
} else if sum > target {
k--
} else {
j++
}
}
}
}
return res
}
// 解法二 暴力解法 O(n^3)
func threeSumClosest1(nums []int, target int) int {
res, diff := 0, 1 << 31
abs := func(x int) int { if x < 0 { return -x; }; return x; }
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) < diff {
diff = abs(nums[i] + nums[j] + nums[k] - target)
res = nums[i] + nums[j] + nums[k]
}
}
}
}
return res
}
func threeSumClosest2(nums []int, target int) int {
sort.Ints(nums)
res, diff := 0, 1 << 31
abs := func(x int) int { if x < 0 { return -x; }; return x; }
for i := range nums {
curr, j, k := nums[i],i + 1, len(nums) - 1
for k > j {
sum := curr + nums[k] + nums[j]
if abs(target - sum) < diff {
res = sum
diff = abs(target - sum)
} else if sum == target {
return sum
} else if sum > target {
k--
} else if sum < target {
j++
}
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [-1,2,1,-4], target = 1
// Output: 2
// Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
fmt.Println(threeSumClosest([]int{-1, 2, 1, -4},1)) // 2
// Example 2:
// Input: nums = [0,0,0], target = 1
// Output: 0
fmt.Println(threeSumClosest([]int{0,0,0},1)) // 0
fmt.Println(threeSumClosest([]int{1,2,3,4,5,6,7,8,9},2)) // 6
fmt.Println(threeSumClosest([]int{9,8,7,6,5,4,3,2,1},2)) // 6
fmt.Println(threeSumClosest1([]int{-1, 2, 1, -4},1)) // 2
fmt.Println(threeSumClosest1([]int{0,0,0},1)) // 0
fmt.Println(threeSumClosest1([]int{1,2,3,4,5,6,7,8,9},2)) // 6
fmt.Println(threeSumClosest1([]int{9,8,7,6,5,4,3,2,1},2)) // 6
fmt.Println(threeSumClosest2([]int{-1, 2, 1, -4},1)) // 2
fmt.Println(threeSumClosest2([]int{0,0,0},1)) // 0
fmt.Println(threeSumClosest2([]int{1,2,3,4,5,6,7,8,9},2)) // 6
fmt.Println(threeSumClosest2([]int{9,8,7,6,5,4,3,2,1},2)) // 6
}