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

To enumerate a subset of specified length without repetition, the best method is to keep the index increasing. (e.g. 0,1,2; 1,3,4)

The brute force enumeration use a 3-for loop which is O(n^3).

Remind the technique we used in 2Sum, we could sort first then start from the begin and the end to get the most closest sum.

So here we put the first num in a for loop, and use the technique in 2Sum to enumerate the other two num. The time complexity is O(n^2). Note the index of the elements in the subset is increasing.

FIRST ERROR: Since we want to find the closest, I firstly wrongly used right-- to change the while loop index. How stupid error it is... I should change the index according to the sum compared with the target.

Code:

lass Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int n = num.size();
if(n < 3) return 0;
int closest = INT_MAX;
int mingap = INT_MAX; sort(num.begin(), num.end());
for(int i = 0; i < n-2; i++)
{
int left = i+1, right = n-1;
while(left < right)
{
int cursum = num[i] + num[left] + num[right];
int gap = abs(target - cursum);
if(gap < mingap)
{
closest = cursum;
mingap = gap;
}
if(cursum < target) left++; // first error
else if(cursum > target) right--;
else return target;
}
}
return closest;
}
};

  

3 3Sum closest_Leetcode的更多相关文章

  1. LeetCode: 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  2. 3Sum algorithm - 非常容易理解的实现 (java)

    原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...

  3. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

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

  5. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  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, 3Sum Closest, 4Sum

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

  8. 16. 3Sum Closest

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

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

随机推荐

  1. 《你不常用的c#之XX》

    你不常用的c#之一>:略谈unsafe http://blog.csdn.net/robingaoxb/article/details/6199508 <你不常用的c#之二>:略谈G ...

  2. [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!

    引言 之前项目需要,查找了office文档在线预览的解决方案,顺便记录一下,方便以后查询. 方案一 直接在浏览器中打开Office文档在页面上的链接.会弹出如下窗口: 优点:主流浏览器都支持. 缺点: ...

  3. 【学习笔记】JAva编程思想之多态

    1.如果java的基类拥有某个已被多次重载的方法名称,那么在导出类中重新定义该方法名称并不会屏蔽在基类的任何版本.因此,无论是在该层或者他的基类中对方法进行定义,重载机制都可以正常工作. 2.使用@O ...

  4. Ubuntu Server 14.04 集成

    方便工作出差显示项目整合了下平时常用软件: OS: Ubuntu Server 14.04 VM:VMware Workstation 12.1.0 (不同版本好像会不兼容) 已经安装软件: 1. s ...

  5. 码云以及git使用

    码云的使用方法以及git的连用 创建公钥的方法 打开码云,点击个人资料---->SSH公钥---->点击怎样生成公钥 SSH Keys ssh keys可以让你在你的电脑和Git@OSC知 ...

  6. java从基础知识(八)泛型

    1.什么是泛型? 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方法 ...

  7. centos7 打开mysql 3306端口并 设置外部访问

    mysql安装后默认是localhost访问,如果需要外部访问可以设置一个新的账号把host改为%,意味着所有ip均可以访问 grant all privileges on *.* to 'outUs ...

  8. UnixBench-linux下的跑分软件

    UnixBench是一款开源的测试 unix 系统基本性能的工具,是比较通用的测试VPS性能的工具.UnixBench会执行一系列的测试,包括2D和3D图形系统的性能,测试的结果不仅仅是CPU,内存, ...

  9. C# 构建动态树

    public class Tree { public Guid Id { get; set; } public string Url { get; set; } public Guid? Parent ...

  10. Linux文件与目录管理

    .      代表此层目录 . .     代表上一层目录 -      代表前一个工作目录 ~     代表"目前用户身份"所在的中文件夹 ~account   代表accoun ...