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 = ...
随机推荐
- 修改织梦data目录名
1.修改include目录下的common.inc.php这个文件.打开文件,找到第24行: define('DEDEDATA', DEDEROOT.'/data'); 把data修改成为您要改的目录 ...
- RAP开发入门-布局管理
布局类继承关系 FillLayout new FillLayout(SWT.VERTICAL/HORIZONTAL)设置竖直/水平填充 RowLayout wrap折行显示.pack自适应布局(布局 ...
- MVC 接收文件
[HttpPost] public ActionResult Layedit() { var files = Request.Files; //获得所上传的所有文件 ) { HttpPostedFil ...
- 用apt-get install一个软件的时候出现错误: 无法解析或打开软件包的列表或是状态文件
用apt-get install一个软件的时候出现了一个错误: E: Encountered a section with no Package: header E: Problem with Mer ...
- java类型和mysql类型的转换
Integer -----> int 11String -----> varchar 20Long -----> bigint 20String - ...
- HMM隐马尔可夫模型(词语粘合)
HMM用于自然语言处理(NLP)中文分词,是用来描述一个含有隐含未知参数的马尔可夫过程,其目的是希望通过求解这些隐含的参数来进行实体识别,说简单些也就是起到词语粘合的作用. HMM隐马尔可夫模型包括: ...
- js 背景从无到黑的渐变 字从白到黑的渐变
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【airtest】报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')),解决方法如下
1. 环境及设备:mac, xcode , iphonex 2. 最近出现一个让人费解的问题,airtest 没跑多长时间,服务就断掉,而且总是报“requests.exceptions.Connec ...
- 【深入理解Java虚拟机】自动内存管理机制——垃圾回收机制
Java与C++之间有一堵有内存动态分配和垃圾收集技术所围成的"高墙",墙外面的人想进去,墙里面的人却想出来.C/C++程序员既拥有每一个对象的所有权,同时也担负着每一个对象生 ...
- [luogu4053 JSOI2007] 建筑抢修 (贪心 优先队列)
传送门 题目描述 小刚在玩JSOI提供的一个称之为"建筑抢修"的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设施受到了严重的损伤 ...