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

问题:给定一个数组和一个整数 n ,求数组中的三个元素,他们的和离 n 最近。

这道题和 3Sum 很相似,解题思路也很相似,先排序,然后采用双指针法依次比较。

由于 3Sum 很相似,思路就不详说。主要说下不同点:

  1. 由于是求最接近值,当 s[i] + s[l] + s[r] - target == 0 的时候,即可返回结果。
  2. 返回的结果是最接近 n 的三个元素之和。
 int threeSumClosest(vector<int>& nums, int target) {

     std::sort(nums.begin(), nums.end());

     int closest = target - nums[] - nums[] - nums[];

     for (int i =  ; i < nums.size(); i++) {

         int l = i + ;
int r = (int)nums.size() - ; int newTarget = target - nums[i]; while (l < r) {
if (nums[l] + nums[r] == newTarget) {
return target;
} int diff = newTarget - nums[l] - nums[r];
if (abs(diff) < abs(closest)) {
closest = diff;
} if (nums[l] + nums[r] < newTarget){
l++;
}else{
r--;
}
}
} return target - closest;
}

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

  4. leetcode 16. 3Sum Closest JAVA

    题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...

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

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

  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】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

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

  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. Linux下U盘的格式化

    一次系统装机带来的烦恼. 之前有一次装centos 系统 ,把一个centos4.8的系统刻录到了一个8G的U盘,之后是centos安装成功了 ,却发现电脑不认识U盘了,试了好多次也没有处理好,刚好今 ...

  2. MongoDB笔记(三)启动命令mongod的参数

    上一节有关访问权限的笔记,是由启动命令mongod的参数auth引发的有关问题,这节就来看看mongod的其他参数 MongoDB启动命令mongod参数说明: 基本配置 --quiet # 安静输出 ...

  3. sass中常用mixin

    //设置字体大小 @mixin font($s:14px,$h:1.5,$f:microsoft yahei){ font:$s/#{$h} $f; } //参数(方向,大小,颜色),默认:向下,10 ...

  4. jquery 去掉重复项(splice,apply,push)

    /* js数组去掉重复项 var somearray = [1,1,2,2,3,3,4,4,'1']; somearray.check(); //somearray will return arr=[ ...

  5. 深入了解relative

    1.relative是自身定位,距原本位置的偏移 2.无侵入布局: 挪动位置,原本位置还在占据,并不会影响其他元素的布局   应用: 实现鼠标拖拽,比自身api好用 3.top/bottom 和 le ...

  6. c语言位运算符

    C语言既具有高级语言的特点,又具有低级语言的功能. 所谓位运算是指进行二进制位的运算. C语言提供的位运算: 运算符   含义  &   按位与  |   按位或  ∧   按位异或  ∽   ...

  7. php获取文件mime类型Fileinfo等方法

    前几天写到使用wordpress xmlrpc api远程发布文章,如果本地服务器的文章库里某一篇待发表的wordpress文章包含图片文件时,就会使用到WordPress上传文件的API metaW ...

  8. 如何写一个像btgoogle一样的12306泄露数据查询

    demo地址:http://www.btgoogle.com/12306/ 圣诞节,12306送给了我们一个大礼物.大约 14w的数据泄露, 看网上都沸沸扬扬的.开始也准备找一个数据库来看看,随后,我 ...

  9. HDU 1532 Drainage Ditches 最大流 (Edmonds_Karp)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 感觉题意不清楚,不知道是不是个人英语水平问题.本来还以为需要维护入度和出度来找源点和汇点呢,看 ...

  10. list和数组之间相互的转化

    list变成数组: String[] str=(String[]) list.toArray(new String[list.size()]); 数组变成list: List<String> ...