1 Star 0 Fork 0

catyMap/AlgorithmNote

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
threeSumClosest.go 529 Bytes
一键复制 编辑 原始数据 按行查看 历史
catyMap 提交于 2021-03-30 22:11 +08:00 . 未归类算法题目提交
package main
import (
"fmt"
"math"
)
func main() {
arr := [] int{0,-4,1,-5}
fmt.Print(threeSumClosest(arr,0))
}
func threeSumClosest(nums []int, target int) int {
n := len(nums)
res := math.MaxInt32
update := func(newres int) {
if Abs(target-newres) < Abs(target-res) {
res = newres
}
}
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for k := j + 1; k < n; k++ {
update(nums[i] + nums[j] + nums[k])
}
}
}
return res
}
func Abs(a int) int{
if a < 0 {
return -a
}
return a
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dogemap/algorithm-note.git
git@gitee.com:dogemap/algorithm-note.git
dogemap
algorithm-note
AlgorithmNote
dc486f96f6c1

搜索帮助