当前位置:网站首页>PHP算法之最接近的三数之和
PHP算法之最接近的三数之和
2022-08-01 22:08:00 【phpstory】
<?php
function threeSumClosest($nums, $target) {
sort($nums);
$count = count($nums);
$ans = $nums[0] + $nums[1] + $nums[2];
for( $i=0;$i<$count;$i++) {
$start = $i+1; $end = $count - 1;
while($start < $end) {
$sum = $nums[$start] + $nums[$end] + $nums[$i];
if(abs($target - $sum) < abs($target - $ans)){
$ans = $sum;
}
if($sum > $target){
$end--;
}
else if($sum < $target){
$start++;
}
else{
return $ans;
}
}
}
return $ans;
}
var_dump(threeSumClosest(array(-1,2,1,-4,0),1));
?>
边栏推荐
- Kubernetes第零篇:认识kubernetes
- 漫长的投资生涯
- Recycling rental system 100% open source without encryption Mall + recycling + rental
- Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
- Prufer序列
- 安全第五次课后练习
- ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
- User Experience | How to Measure User Experience?
- 线程池分析
猜你喜欢

毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你

Delicious this year
![[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)](/img/26/4c3080f1b21efb9401d8c3a55bc15d.png)
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)

还在纠结报表工具的选型么?来看看这个

blender3.2.1 unit setting

小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告

不卷了!入职字节跳动一周就果断跑了。

AIDL communication

seaborn笔记:可视化统计关系(散点图、折线图)

【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
随机推荐
2022 版 MySQL 巅峰教程,收藏好,慢慢看
【C语言实现】最大公约数的3种求法
1. @Component注解的原理剖析
Postman 批量测试接口详细教程
[机缘参悟-58]:《素书》-5-奉行仁义[遵义章第五]
2022 edition of MySQL tutorial, top collection good, take your time
小程序中的多表联合查询
Kubernetes Scheduler全解析
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
力扣第 304 场周赛复盘
How to prevent governance attacks in DAOs?
familiar friend
leetcode 204. Count Primes 计数质数 (Easy)
高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
KMP 字符串匹配问题
Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
企业公众号文章写作方向:如何写出读者认可的优质内容
移动端人脸风格化技术的应用
SOM Network 2: Implementation of the Code