题目

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使得:

\[min = arg \ min | \ target - min \ | \quad s.t. \quad min =a+b+c
\]

即求出使得$| \ 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]最接近的三数之和的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 力扣——3sum closest(最接近的三数之和)python 实现

    题目描述: 中文: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一 ...

  4. 【LeetCode每天一题】3Sum Closest(最接近的三数和)

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  5. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  6. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  7. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  8. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

  9. lintcode-59-最接近的三数之和

    59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...

随机推荐

  1. JavaScript的面向对象

    JavaScript的对象 对象是JavaScript的一种数据类型.对象可以看成是属性的无序集合,每个属性都是一个键值对,属性名是字符串,因此可以把对象看成是从字符串到值的映射.这种数据结构在其他语 ...

  2. (转载)更新到Retrofit2的一些技巧

    更新到Retrofit2的一些技巧 作者 小武站台 关注 2016.02.22 22:13* 字数 1348 阅读 1621评论 0喜欢 5赞赏 1 原文链接:Tips on updating to ...

  3. 搭建 Lepus 天兔 监控MySQL

    Part1: Lepus安装需要Lamp环境,lepus官网手册也建议采用XAMPP的方式安装,lepus也是在XAMPP上进行研发的 注意xampp会把apache,mysql,php都安装,所以要 ...

  4. 基于SLIC分割的特征点检测

    一:pipeLIne (1):基于模型的pose估计综述: 对于一个3D模型,可以投影到平面,得到不同的位姿,而pose识别是利用所见的2.5D图像,来估计模型,并同时识别出位姿. 3D模型投影时注意 ...

  5. 读书笔记「Python编程:从入门到实践」_2.变量和简单数据类型

    做了大半年RPA了,用的工具是Kapow. 工作没有那么忙,不想就这么荒废着,想学点什么.就Python吧. 为期三个月,希望能坚持下来. 2.1 变量的命名和使用 变量名只能包含字母.数字和下划线. ...

  6. App测试- adb monkey测试

    一. 安装和配置SDK 1. 下载Android SDK并解压.如下图:(如果不存在tool和platform_tool,请点击SDK Manager在线下载和更新) 2.下载完成后,配置SDK环境变 ...

  7. React+Antd遇到的坑

    第一次尝试React+antd,发现果然不愧是传说中的坑货,一个又一个坑.必须要记录. react + antd,都是最新版本,使用npm和yarn各种add,build,start 1. 资源文件, ...

  8. Surrogate data 代理数据

    附一篇science论文,待啃: 附Surrogate time series and fields,matlab:https://www.sogou.com/link?url=DSOYnZeCC_p ...

  9. Webstorm 破解2017.1 for Mac

    废话不多说,改了去年分享2016版本的文章,给同学们带来2017.1版本的Mac版本.(win版本网上很多,我这里就不贴出来了). 1.去官仿下载最新的版本  https://www.jetbrain ...

  10. gradle多模块构建集成swagger

    1.首先说一下软件的版本:springboot:1.5.2:springcloud:D-SR1:swaager2:2.6.0:gradle:4.5.工程模块是分开的单独的entity,api,mapp ...