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. JavaScript原型链demo

    function Person(name){ this.name = name; } Person.prototype = { say: function(){ alert('hi'); }, say ...

  2. C#XML创建与节点对象引用

    我们在创建xml过程中会遇到不同的级别有相同节点的情况.如下面的xml: <?xml version="1.0" encoding="GBK"> & ...

  3. 面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0

    package com.Amazon.interview; /** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www.c ...

  4. 用Python和Django实现多用户博客系统(二)——UUBlog

    这次又更新了一大部分功能,这次以app的形式来开发. 增加博客分类功能:博客关注.推荐功能(ajax实现) 增加二级频道功能 更多功能看截图及源码,现在还不完善,大家先将就着看.如果大家有哪些功能觉的 ...

  5. C++ 11 笔记 (五) : std::thread

    这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...

  6. 安装pip之后,执行pip,提示:unknown or unsupported command install

    安装pip之后,在dos命令框中输入pip,提示:unknown or unsupported command install,搜索之后,立马在:http://stackoverflow.com/qu ...

  7. Navigation bar 的注意事项

    Navigation bar 的注意事项 Bar button item 使用 button 作为 custom view,初始化 isEnabled 为 false,注意顺序 需要设置 bar bu ...

  8. iOS项目架构文档

    设计的项目架构主要引用MVVM+MVC架构,并以功能模块分级.以下为目录结构. 初级目录: 我们只需要关注SGZH文件夹下的目录,其他为Xcode管理的目录.可以看到此目录为项目初级目录,我们开发过程 ...

  9. docker下使用caffe的命令记录

    查看所有的images sudo docker images 利用某个image生成container sudo docker run -it --net=host -v /home/tingting ...

  10. webservice 的权限验证

    1. http://blog.csdn.net/jaune161/article/details/25602655 2. http://wcp88888888.iteye.com/blog/13993 ...