mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
添加 problem 16
This commit is contained in:
51
Algorithms/16. 3Sum Closest/16. 3Sum Closest.go
Normal file
51
Algorithms/16. 3Sum Closest/16. 3Sum Closest.go
Normal file
@ -0,0 +1,51 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func threeSumClosest(nums []int, target int) int {
|
||||
n, res, diff := len(nums), 0, math.MaxInt32
|
||||
if n > 2 {
|
||||
sort.Ints(nums)
|
||||
for i := 0; i < n-2; i++ {
|
||||
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
|
||||
}
|
||||
|
||||
func threeSumClosest_(nums []int, target int) int {
|
||||
res, difference := 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) < difference {
|
||||
difference = abs(nums[i] + nums[j] + nums[k] - target)
|
||||
res = nums[i] + nums[j] + nums[k]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func abs(a int) int {
|
||||
if a > 0 {
|
||||
return a
|
||||
}
|
||||
return -a
|
||||
}
|
63
Algorithms/16. 3Sum Closest/16. 3Sum Closest_test.go
Normal file
63
Algorithms/16. 3Sum Closest/16. 3Sum Closest_test.go
Normal file
@ -0,0 +1,63 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question16 struct {
|
||||
para16
|
||||
ans16
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para16 struct {
|
||||
a []int
|
||||
target int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans16 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem16(t *testing.T) {
|
||||
|
||||
qs := []question16{
|
||||
|
||||
question16{
|
||||
para16{[]int{-1, 0, 1, 1, 55}, 3},
|
||||
ans16{2},
|
||||
},
|
||||
|
||||
question16{
|
||||
para16{[]int{0, 0, 0}, 1},
|
||||
ans16{0},
|
||||
},
|
||||
|
||||
question16{
|
||||
para16{[]int{-1, 2, 1, -4}, 1},
|
||||
ans16{2},
|
||||
},
|
||||
|
||||
question16{
|
||||
para16{[]int{1, 1, -1}, 0},
|
||||
ans16{1},
|
||||
},
|
||||
|
||||
question16{
|
||||
para16{[]int{-1, 2, 1, -4}, 1},
|
||||
ans16{2},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 16------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans16, q.para16
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, threeSumClosest(p.a, p.target))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
24
Algorithms/16. 3Sum Closest/README.md
Normal file
24
Algorithms/16. 3Sum Closest/README.md
Normal file
@ -0,0 +1,24 @@
|
||||
# [16. 3Sum Closest](https://leetcode.com/problems/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:
|
||||
|
||||
```c
|
||||
Given array nums = [-1, 2, 1, -4], and target = 1.
|
||||
|
||||
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个数组,要求在这个数组中找出 3 个数之和离 target 最近。
|
||||
|
||||
这一题看似和第 15 题和第 18 题很像,都是求 3 或者 4 个数之和的问题,但是这一题的做法和 15,18 题完全不同。
|
||||
|
||||
这一题的解法是用两个指针夹逼的方法。先对数组进行排序,i 从头开始往后面扫。这里同样需要注意数组中存在多个重复数字的问题。具体处理方法很多,可以用 map 计数去重。这里笔者简单的处理,i 在循环的时候和前一个数进行比较,如果相等,i 继续往后移,直到移到下一个和前一个数字不同的位置。j,k 两个指针开始一前一后夹逼。j 为 i 的下一个数字,k 为数组最后一个数字,由于经过排序,所以 k 的数字最大。j 往后移动,k 往前移动,逐渐夹逼出最接近 target 的值。
|
||||
|
||||
|
||||
这道题还可以用暴力解法,三层循环找到距离 target 最近的组合。具体见代码。
|
Reference in New Issue
Block a user