算法题丨3Sum Closest
描述
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
Return the sum of the three integers. You may assume that each input would have exactly one solution.
示例
Given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
算法分析
难度:中
分析:给定整型数组中和一个指定的目标整型值,从数组中找到3个元素,满足3个元素之和最接近目标值,返回结果为3个元素之和。
思路:大体思路类似3Sum算法,也是先将数组排序,然后开始遍历,3个元素分别是当前遍历的元素、夹逼开始元素默认当前元素后面的元素,夹逼结束元素默认数组最后的元素,通过夹逼开始元素递增和夹逼结束元素递减来实现夹逼:
1. 如果这3个元素之和比目标值大,则需要夹逼结束元素值变小,才有可能接近目标值,所以夹逼结束元素递减;
2. 如果这3个元素之和不比目标值大,则需要夹逼开始元素值变大,才有可能接近目标值,所以夹逼开始元素递增;
本次遍历结束后,再判断本次3元素之和目前记录的最接近之和比较,取更接近的做为返回结果,然后继续遍历,直至遍历结果,获得返回结果。
代码示例(C#)
public int ThreeSumClosest(int[] nums, int target)
{
int res = nums[0] + nums[1] + nums[nums.Length - 1];
//排序后遍历
Array.Sort(nums);
for (int i = 0; i < nums.Length - 2; i++)
{
//从当前后面的元素和最后一个元素,两边夹逼
int lo = i + 1, hi = nums.Length - 1;
while (lo < hi)
{
int sum = nums[i] + nums[lo] + nums[hi];
if (sum > target)
{
hi--;
}
else
{
lo++;
}
//如果此次遍历的3个元素的和更接近,则赋值返回的结果
if (Math.Abs(sum - target) < Math.Abs(res - target))
{
res = sum;
}
}
}
return res;
}
复杂度
- 时间复杂度:O (n²).
- 空间复杂度:O (1).
附录
算法题丨3Sum Closest的更多相关文章
- 算法题丨3Sum
描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 算法题丨Two Sum
描述 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 算法题丨4Sum
描述 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 算法题丨Longest Consecutive Sequence
描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- 算法题丨Remove Element
描述 Given an array and a value, remove all instances of that value in-place and return the new length ...
- 算法题丨Move Zeroes
描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...
- 算法题丨Next Permutation
描述 Implement next permutation, which rearranges numbers into the lexicographically next greater perm ...
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
随机推荐
- CentOS 静态IP分配,提示Error, some other host already uses address解决办法
(一)第一 修改 ifup-eth vi /etc/sysconfig/network-scripts/ifup-eth #if ! /sbin/arping -q -c 2 -w 3 -D -I $ ...
- Maven错误信息:Missing artifact jdk.tools:jdk.tools:jar:1.6
在pom.xml中添加依赖: <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.too ...
- C# Redis实战(二)
二.Redis服务 在C# Redis实战(一)中我将所有文件拷贝到了D盘redis文件夹下,其中redis-server.exe即为其服务端程序,双击即开始运行,如图 可以 ...
- CSS3字体发光效果
text-shadow 该属性为文本添加阴影效果 text-shadow: h-shadow v-shadow blur color; h-shadow: 水平阴影的位置(阴影水平偏移量),可为负值, ...
- 一年java工作经验的面试题总结(持续更新中)
本人是17年6月份毕业的,3月份出来实习,算起来也是工作一年了吧,金三银四,博主也考虑换一份工作,于是最近面试了几家,总结一下面试中的问题,大家一起交流学习. 第一次面试 ①说下java类的加载 ② ...
- Java设计模式(六)Adapter适配器模式
一.场景描述 “仪器数据采集器”包含采集数据以及发送数据给服务器两行为,则可定义“仪器数据采集器”接口,定义两方法“采集数据capture”和“发送数据sendData”. “PDF文件数据采集器”实 ...
- shiro权限框架(三)
三.身份验证 身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份 ID 一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明 在 shiro 中,用户需要提供 principa ...
- echarts地图的引用
最近是跟echarts杠上了 所在公司是搞数据的 所以身为前端的我 就必须使用echarts将数据展示出来 ,进公司一周 ,前前后后大概用了八九种echarts图,我举得最难的就是引用的地图,因为刚开 ...
- 多线程 Synchronized关键字和Lock
Synchronized 分为实例锁和全局锁. 实例锁为 synchronized(this) 和 非static synchronized方法. 也加对象锁. 只要一个线程访问这类的一个syn ...
- vs连接Oracle 客户端库时引发 BadImageFormatException
报错:Oracle 客户端库时引发 BadImageFormatException如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式? 解决方案:http://www.cnblog ...