Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

    For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

题意:给定值target,在数列中找到三个数,它们的和最接近这个target。

思路:类似于Two sum、3sum还有本题,都可以先排序后,用夹逼法则:使用两个指针,从前向后,从后向前的遍历,找到符合条件的情况。为什么要使用这种方法了?针对本题,若是固定一个,然后再固定一个,通过移动最后一个指针,找到最小的差,然后,在重新将第二个指针移动一个位置,低三个指针,再重新遍历,这样耗时严重。利用好,已将数组排序这一条件,固定一个数,剩下的两个数分别从头和尾向中间遍历,若是三者的和大于target,则尾指针向左移动,减小对应的值,否则前指针右移增大对应的值,从而增大三者和。与此同时,更新和target最小的差和对应的sum。代码如下:

 class Solution {
public:
int threeSumClosest(vector<int> &num, int target)
{
int sum=num[]+num[]+num[];
int diff=abs(sum-target);
sort(num.begin(),num.end()); for(int i=;i<num.size();++i)
{
int l=i+,r=num.size()-;
while(l<r)
{
int temp=num[i]+num[l]+num[r];
int newDiff=abs(temp-target); if(diff>newDiff)
{
diff=newDiff;
sum=temp;
}
if(temp>target)
r--;
else
l++;
}
}
return sum;
}
};

[Leetcode] 3sum-closest 给定值,最为相近的3数之和的更多相关文章

  1. [LeetCode] 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]3Sum Closest题解

    3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...

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

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

  4. [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  5. #leetcode刷题之路16-最接近的三数之和

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

  6. [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  7. Leetcode 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  8. LeetCode 3Sum Closest (Two pointers)

    题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  9. LeetCode 16 3Sum Closest (最接近target的3个数之和)

    题目链接 https://leetcode.com/problems/3sum-closest/?tab=Description     Problem : 找到给定数组中a+b+c 最接近targe ...

随机推荐

  1. js实现监听浏览器窗口大小改变事件

    window.onresize = function(){   }

  2. python中如何统计一个类的实例化对象

    类中的静态变量 需要通过类名.静态变量名 来修改 :通过对象不能修改 python中如何统计一个类的实例化对象?? class Person: #静态变量count,用于记录类被实例化的次数 coun ...

  3. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  4. PHP中的面向对象魔术方法大全

    1.__construct  构造方法 2.__destruct  析构方法 3.__get 获取成员值 4.__set 设定成员值 5.__isset 判断成员值 6.__unset unset成员 ...

  5. Matplotlib 子图的创建

    在matplotlib中,整个图像为一个Figure对象 在Figure对象中可以包含一个或者多个Axes对象  每个Axes对象相当于一个子图了 每个Axes(ax)对象都是一个拥有自己坐标系统的绘 ...

  6. Go语言中的UDP应用

    Go语言中的UDP应用 Go语言中使用UDP是很方便的,net包提供了UDP和TCP的功能,这里使用UDP做了一个UDP广播,然后接收各个设备的返回信息.实现起来很快,总体感觉比使用C#中的UDP更优 ...

  7. C++ vector二维数组

    C++ 构建二维动态数组 int **p; p = ]; //注意,int*[10]表示一个有10个元素的指针数组 ; i < ; ++i) { p[i] = ]; } 这样就构成10*5的数组 ...

  8. Big Truck

    Photo by Phil Whitehouse Your boss has hired you to drive a big truck, transporting items between tw ...

  9. 为什么我要放弃javaScript数据结构与算法(第三章)—— 栈

    有两种结构类似于数组,但在添加和删除元素时更加可控,它们就是栈和队列. 第三章 栈 栈数据结构 栈是一种遵循后进先出(LIFO)原则的有序集合.新添加的或待删除的元素都保存在栈的同一端,称为栈顶,另一 ...

  10. 2 socket UDP通信

    1 socket套接字  class 对象 In [1]: import socket In [2]: help(socket.socket) class socket(_socket.socket) ...