题目

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. P1284 三角形牧场

    题目描述 和所有人一样,奶牛喜欢变化.它们正在设想新造型的牧场.奶牛建筑师Hei想建造围有漂亮白色栅栏的三角形牧场.她拥有N(3≤N≤40)块木板,每块的长度Li(1≤Li≤40)都是整数,她想用所有 ...

  2. 基于S3C2440数码相框

    [参考]韦东山 教学笔记 1. 程序框架1.1 触摸屏: 主按线程,通过socket发给显示进程 --------------------------- 封装事件:ts线程 按键线程 -------- ...

  3. MyEclipse 中的一些快捷键

    @import url(/css/cuteeditor.css); ------------------------------------- MyEclipse 快捷键1(CTRL) ------- ...

  4. How to share memory between services and user processes?

    除了必要的InitializeSecurityDescriptor和SetSecurityDescriptorDacl, 内存映射文件名必须GLOBAL开头.

  5. 查看tomcat的内存情况

    查看现有tomcat的内存大小情况 1.启动tomcat 2.访问 http://localhost:8080/manager/status ,并输入您在安装tomcat时输入的用户与口令,如 adm ...

  6. Associated Values & enum

    it is sometimes useful to be able to store associated values of other types alongside these case val ...

  7. Mac 查看 剪贴板/剪切板/粘贴板 内容与格式

    命令行形式 osascript -e 'clipboard info' GUI 形式 Finder->编辑->显示剪贴板 图示:

  8. 洛谷P2296 寻找道路_简单BFS

    Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; co ...

  9. php 流

    php:// — 访问各个输入/输出流(I/O streams) 说明 PHP 提供了一些杂项输入/输出(IO)流,允许访问 PHP 的输入输出流.标准输入输出和错误描述符, 内存中.磁盘备份的临时文 ...

  10. Python编程:从入门到实践 - pygal篇 - Die

    掷骰子 # die.py 骰子类 from random import randint class Die(): """表示一个骰子的类""" ...