3 3Sum closest_Leetcode
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的更多相关文章
- 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 ...
- 3Sum algorithm - 非常容易理解的实现 (java)
原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 16. 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 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 ...
随机推荐
- jquery插件扩展的学习
jquery插件的学习可以点击这里 举个例子 //首先先来一个插件 (function($){ $.fn.extent({ bigfont:function(){ return this.css('f ...
- APEX初步
APEX是SFDC中用于开发的语言.语法上类似JAVA等面向对象的语言,运行起来类似数据库中的存储过程.可以在SFDC事件中添加业务逻辑,操作相关数据和用在Visual Force页面中. 保存,编译 ...
- 初识exception
一.exception的分类 根据此exception(异常)是否可以打断正在执行的指令,可以将exception分为 asynchronous exception 和 synchronous exc ...
- 转载:MySQL 语句大全:创建、授权、查询、修改等
本文转载>这里 一.用户创建.权限.删除 1.连接MySql操作 连接:mysql -h 主机地址 -u 用户名 -p 用户密码 (注:u与root可以不用加空格,其它也一样)断开:exit ( ...
- javascript 技巧总结积累(正在积累中)
1.文本框焦点问题 onBlur:当失去输入焦点后产生该事件 onFocus:当输入获得焦点后,产生该文件 Onchange:当文字值改变时,产生该事件 Onselect:当文字加亮后,产生该文件 & ...
- 关于tkCommand的各种事件的解释
superclass for callback/observer methods vtkCommand is an implementation of the observer/command des ...
- sql 脚本编写之路 常用语句(一)
1.用一个表中的某一列更新另外一个表的某些列: for ACCESS 数据库: update a, b set a.name=b.name1 where a.id=b.id for SQL Serve ...
- Android之Linearlayouy线性布局
写了个小例子xml代码如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout x ...
- 高程三:Array
一:Array数组 1.Array.isArray(参数) 检测是否是数组,*不兼容IE8,兼容IE9及以上.Chrome.Firefox等,要兼容IE8,可以用 Object.prototype.t ...
- Cvim的安装与使用
一.安装cvim插件 第一步:下载cvim的安装包 在linux系统下的浏览器firefox.chrome浏览器中打开下面链接 www.vim.org/scripts/download_script. ...