16. 3Sum Closest[M]最接近的三数之和
题目
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:
Given array nums = [-1, 2, -1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
思路
思路:三指针法
对于一个数组S,求数组中三个数a、b、c的和,使之最接近target。将问题抽象化为,求一个min使得:
\]
即求出使得$| \ target - min \ | $时min的值。
三指针法,见题[15]。三指针方法的关键是:
- 判断三个指针的移动的方向与条件(首先对数组排序)
- 如果min < target,说明min太小了,不够接近target,将中间指针往大的方向移动
- 如果min = target,由于只有一个结果,故可以直接返回
- 如果min < target,说明min太大了,超出了target,将尾指针往小的方向移动
- 状态的更新
- 保证min始终是指针移动过程中a+b+c的最小值。
- 注意
- 每次只移动一个指针
C++
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(),nums.end()); //对数组排序
int min=nums[0]+nums[1]+nums[2]; //初始化最小值
for(int pBegin=0;pBegin<nums.size();pBegin++){
int pMid = pBegin+1;//中间指针
int pEnd=nums.size()-1;//尾指针
int sub=target-nums[pBegin];
while(pMid<pEnd){
if(abs(sub-nums[pMid]-nums[pEnd]) < abs(target-min)) //更新最接近target时的和
min = nums[pBegin]+nums[pMid]+nums[pEnd];
if(nums[pMid]+nums[pEnd] == sub){
return target;
}
else if(nums[pMid]+nums[pEnd] > sub){
pEnd--;
}
else{
pMid++;
}
}
}
return min;
}
Python
class Solution(object):
def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
nums.sort()
minVal = nums[0]+nums[1]+nums[2]
for pBegin in range (len(nums)):
pMid = pBegin + 1
pEnd = len(nums) - 1
subVal = target - nums[pBegin]
while pMid < pEnd:
if abs(subVal - nums[pMid] - nums[pEnd]) < abs(target - minVal):
minVal = nums[pBegin] + nums[pMid] + nums[pEnd]
if nums[pMid] + nums[pEnd] == subVal:
return target
elif nums[pMid] + nums[pEnd] > subVal:
pEnd -= 1
else:
pMid += 1
return minVal
16. 3Sum Closest[M]最接近的三数之和的更多相关文章
- LeetCode 16. 3Sum Closest. (最接近的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode OJ:3Sum Closest(最接近的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 力扣——3sum closest(最接近的三数之和)python 实现
题目描述: 中文: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一 ...
- 【LeetCode每天一题】3Sum Closest(最接近的三数和)
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- Leetcode题库——16.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
- lintcode-59-最接近的三数之和
59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...
随机推荐
- Java单例模式解析(收藏)
在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单例设计模式详细的探讨一下. 所谓单例模式,简单来说,就是在整个应用中保证只有一个类的实例存在.就 ...
- web拼图错误分析
老师要求用web制作一个拼图游戏. 发现的问题:点击随机生成拼图的按钮后,打乱的图片会出现无法还原的情况. 发现过程:每次生成一个拼图后会测试它怎么拼回去,结果发现有时候拼不回去. 数学原理:如果两个 ...
- html5左右滑动页面效果实现
The Demo of h5 slider achiev by Myself 主要思路: 设置一个容器container,然后里面有几个page,获取到屏幕的宽度并将其赋值给page,然后contai ...
- Dictionary 小知识
Dictionary<string, string>是一个泛型 他本身有集合的功能有时候可以把它看成数组 他的结构是这样的:Dictionary<[key], [value]> ...
- Spring DATA MongoDB @DBref查询,or和and联合查询
@DBref文档关联,在按该类型查询的时候,在字段名后加上关联表的字段名即可,如: Criteria.where("bloggroup.$id"), $id代表关联表的oid字段. ...
- 【转载】java调用C++写的DLL
用java调用C++写的DLL一直以来都是一个比较麻烦但又很常见的问题. 我们知道,使用 JNI 调用 .dll/.so 共享类库是非常非常麻烦和痛苦的. 如果有一个现有的 .dll/.so 文件,如 ...
- HighCharts 图表插件 自定义绑定 时间轴数据
HighCharts 图表插件 自定义绑定 时间轴数据,解决时间轴自动显示数据与实际绑定数据时间不对应问题! 可能要用到的源码片段:http://code.662p.com/list/14_1.htm ...
- sql语句参数化问题
select @PageSize * from tets SELECT 在WHERE 之前都不能参数化. TOP 只能做字符串运行.
- preparedStatement平台:
public class cs{ public static void main(String[] args){ try{ class.forName("com.mysql.jdbc.Dri ...
- 路飞学城Python-Day186
Evernote Export 持续集成 持续集成,简单的说就是持续集成频繁的将代码集成到主干,它的好处主要有1.快速发现错误,没完成一点更新,就集成到主干,可以快速发现错误,定位错误也会比较容易,2 ...