16. 3Sum Closest
Medium

131696FavoriteShare

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

【题解】:这个题我是真的卡了好几天,主要是因为上一道题没用用指针的方式搜索也过了。。。可以去看我上一道题的题解,这个题,很关键的理解点是,要把数据进行排序,然后再固定一个数,另外两个数就可以通过指针的方式搜索了
这道题让我们求最接近给定值的三数之和,是在之前那道 3Sum 的基础上又增加了些许难度,那么这道题让我们返回这个最接近于给定值的值,即我们要保证当前三数和跟给定值之间的差的绝对值最小,所以我们需要定义一个变量 diff 用来记录差的绝对值,然后我们还是要先将数组排个序,然后开始遍历数组,思路跟那道三数之和很相似,都是先确定一个数,然后用两个指针 left 和 right 来滑动寻找另外两个数,每确定两个数,我们求出此三数之和,然后算和给定值的差的绝对值存在 newDiff 中,然后和 diff 比较并更新 diff 和结果 closest 即可,代码如下:
 class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int diff = numeric_limits<int>::max();
int Final;
sort(nums.begin(),nums.end());
for(int i = ; i < nums.size()-; i++){
int tm1 = nums[i];
int left = i+; int right = nums.size()-;
while(left<right){
int sum = tm1+nums[left]+nums[right];
if(abs(target-sum) < diff){
diff = abs(target-sum) ;
Final = sum;
}
if(target==sum) return target;
else if(target < sum) right--;
else left++;
}
}
return Final;
}
};

Leetcode 16. 3Sum Closest(指针搜索)的更多相关文章

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

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

  2. [LeetCode] 16. 3Sum Closest 最近三数之和

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

  3. [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 ...

  4. Array + two points leetcode.16 - 3Sum Closest

    题面 Given an array nums of n integers and an integer target, find three integers in nums such that th ...

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

  6. Java [leetcode 16] 3Sum Closest

    题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...

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

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

  9. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

随机推荐

  1. ARP协议基础

    ARP 什么是ARP协议 ARP协议是能够根据IP地址解析出该IP地址所在设备的MAC地址,叫(Address Resolution Protocol)地址解析协议 ARP地址的工作流程 当一台主机A ...

  2. [转帖]kafka入门:简介、使用场景、设计原理、主要配置及集群搭建

    kafka入门:简介.使用场景.设计原理.主要配置及集群搭建 http://www.aboutyun.com/thread-9341-1-1.html 还没看完 感觉挺好的. 问题导读: 1.zook ...

  3. C++中对象的构造顺序

    1,C++ 中的类可以定义多个对象,那么对象构造顺序是怎样的? 1,很多的 bug 是由对象的构造顺序造成的,虽然它不难: 2,对象的构造往往和构造函数牵涉在一起,构造函数的函数体又可能由非常复杂的程 ...

  4. 02:django model数据库操作

    Django其他篇 目录: 1.1 Django中使用MySQL 1.2 创建表 1.3 Django一对多表结构操作 1.4 Django多对多表结构操作 1.5 一大波Model操作 1.6 Mo ...

  5. CodeForces - 714E + POJ - 3666 (dp严格单调递增与非严格单调递增)

    左偏树 炒鸡棒的论文<左偏树的特点及其应用> 虽然题目要求比论文多了一个条件,但是……只需要求非递减就可以AC……数据好弱…… 虽然还没想明白为什么,但是应该觉得应该是这样——求非递减用大 ...

  6. 03-Django-models

    # Models 模型 - ORM - ObjectRelationMap : 把面向对象思想转换成关系数据库思想.操作上把类等价于表格 - 类对应表格 - 类中的属性对应表中的字段 - 在应用中的m ...

  7. vue实现动画和css3动画属性

    一.vue动画实现原理: 动画的实现,必须通过元素的显示隐藏或销毁创建.v-show  v-if vue中如果需要使用动画的时候,需要使用一个内置组件transition组件 该组件有一个name属性 ...

  8. mysql复习(1)基本CRUD操作

    一.这段时间在学校,把之前的东西都好好捡起来. 0.下面介绍Mysql的最基本的增删改查操作,很多IT工作者都必须掌握的命令,也是IT面试最常考的知识点.在进行增删改查之前,先建立一个包含数据表use ...

  9. Spring基础09——Bean的自动装配

    1.XML配置的Bean自动装配 SpringIOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,而不需要手工去指定要装配的Bean,a ...

  10. Apache 网站日志分析

    1.获得访问前 10 位的 ip 地址 [root@apache ~]# cat access_log |awk '{print $1}'|sort|uniq -c|sort -nr|head -10 ...