题面

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.

给定数组,找出并返回最接近target的三个元素的和。可以假设,只有一个解。

样例

Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

思路

我们在15题 3Sum中做过,三数加和为target的问题,采用了Two-Point逼近的方法。本题我们稍加改动就可以解决。

1. 数组排序,固定一个元素i,通过两点法去[i+1, size()-1]中搜索另外两个数字,先计算他们的和;

2. 若sum == target,直接返回;若不等,就需要判断sum与 我们给的初值res 那个更加接近target,即判断 abs(sum - target)  与 abs(res - target)的大小,对res进行更新,另外注意 l 和 r 的更新。

3. 返回res.

源码

 class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
if(len < )
return ;
//数组升序排序
sort(nums.begin(), nums.end());
int res = nums[]+nums[]+nums[];
for(int i=; i<len; i++)
{
if(i> && nums[i]==nums[i-])
i++;//避免i的重复,不必要
int l = i+, r = len-;
while(l < r)//两点法搜搜
{
int sum = nums[i] + nums[l] + nums[r];
if(sum == target)
return sum;
else if(sum > target)
{
if(abs(sum - target) < abs(res - target))
res = sum;
r--;
}
else
{
if(abs(sum - target) < abs(res - target))
res = sum;
l++;
}
}
}
return res;
}
};

Array + two points leetcode.16 - 3Sum Closest的更多相关文章

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

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

  2. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

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

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

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

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

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

  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 [Difficulty: Medium]

    题目 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 ☆☆☆

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

随机推荐

  1. 取用户中文名 FDM_CUST_USER_NAME_READ_SINGLE

    DATA:lv_first TYPE ad_namefir,      lv_last  TYPE ad_namelas,      lv_full  TYPE ad_namtext.   CALL  ...

  2. tk mybatis动态sql中过滤不使用的字段

    实体字段如下 @Data @NoArgsConstructor @AllArgsConstructor @Builder /*** * app图标 */ @JsonFormat public clas ...

  3. (十七)Centos之安装配置tomcat8

    第一步:下载Tomcat8压缩包 进入 http://tomcat.apache.org/download-80.cgi 下载tar.gz压缩包 第二步:用ftp工具把压缩包上传到/home/data ...

  4. (六)Centos之目录作用介绍

    我们先切换到系统根目录 / 看看根目录下有哪些目录 这里首先看下 根目录/ 下的 bin 和 sbin: 在user下也有bin和sbin 根目录下的bin和sbin,usr目录下的bin和sbin, ...

  5. (十七)jdbc(Java Data Base Connectivity,java数据库连接)基础使用

    一.JDBC相关概念介绍 1.1 JDBC介绍 SUN公司为了简化.统一对数据库的操作,定义了一套Java操作数据库的规范(接口),称之为JDBC.这套接口由数据库厂商去实现,这样,开发人员只需要学习 ...

  6. vmware安装centos7.5、配置网卡、环境配置

    1.vmware安装centos7.5虚拟机    参考连接: https://blog.csdn.net/guo_ridgepole/article/details/78973763 可能遇到的问题 ...

  7. css 命名规范参考[转]

    命名空间 另外最好的实践就是当命名你的类名的时候,使用命名空间前缀来进行分类.这些前缀会在你的命名前添加一组字符,但是这个值能立刻标记每一个类的目的,在你看 HTML 或者样式的时候是很需要的.我使用 ...

  8. react做的简单的购物车

    ###第一步 :首先电脑上已经安装react的脚手架 cnpm  install    create-react-app   -g ###第二步 :创建项目 creact-react-app   项目 ...

  9. php open_basedir的使用与性能分析

    php open_basedir的使用与性能分析 使用方法 <pre>/*限制打开的目录*/ini_set('open_basedir', __DIR__.'/');</pre> ...

  10. python线程定时器Timer(32)

    相对前面几篇python线程内容而言,本片内容相对比较简单,定时器 – 顾名思义,必然用于定时任务. 一.线程定时器Timer原理 原理比较简单,指定时间间隔后启动线程!适用场景:完成定时任务,例如: ...