题目

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. Educational Codeforces Round 33

    # Who = Penalty * A B C D E F 479 arkethos 4 247   + 00:08 + 00:19 +1 00:59 +2 01:41     479  ne-leo ...

  2. 拖动盒子demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 4185 Oil Skimming 最大匹配 奇偶建图

    题目大意: 统计相邻(上下左右)的‘#’的对数. 解法: 与题目hdu1507 Uncle Tom's Inherited Land*类似,需要用奇偶建图.就是行+列为奇数的作为X集合,偶尔作为Y集合 ...

  4. 电商物流仓储WMS业务流程

    电商物流仓储WMS业务流程 SKU是什么意思?  一文详解电商仓储管理中SKU的含义 从货品角度看,SKU是指单独一种商品,其货品属性已经被确定.只要货品属性有所不同,那么就是不同的SKU. PO信息 ...

  5. UVa 1585 待解决

    是在遇到第一个ooxx的时候会出错,会少算一个1 #include<stdio.h> int main() { int i,k=0,sum=0; char a[100]={"oo ...

  6. Angular ui-router的常用配置参数详解

    一.$urlRouterProvider服务 $urlRouterProvidfer负责监听$location,当$location变化时,$urlRouterProvider将在规则列表中查找匹配的 ...

  7. 00--C++牛人的博客

    那些C++牛人的博客 这篇文章来自转载,转载的网址找不到了,   如果有人知道,可以在下面评论,   非常感谢,更感谢原作者. 现整理收集C++世界里那些“牛人”的个人博客.凡三类:一是令人高山仰止的 ...

  8. 常用 CSS 选择器

    // css 读取顺序从右到左,符合要求的都会匹配 // 通配符选择器 -- 选择所有元素 * // 通配符选择器 -- 选择某个元素下的所有元素 .demo * // 元素选择器 html,body ...

  9. 【转】DBSCAN密度聚类算法

    DBSCAN(Density-Based Spatial Clustering of Applications with Noise,具有噪声的基于密度的聚类方法)是一种很典型的密度聚类算法,和K-M ...

  10. 原生js实现form表单序列化

    当我们有form表单而且里面的表单元素较多时,咱们总不能一个个去获取表单元素内的值来进行拼接吧!这样会很让人蛋疼!为了方便与后台交互并且提高自己的开发效率,并且不让你蛋疼:我们一起用原生来写一个表单序 ...