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 ...
随机推荐
- Nodejs事件引擎libuv源码剖析之:句柄(handle)结构的设计剖析
声明:本文为原创博文,转载请注明出处. 句柄(handle)代表一种对持有资源的索引,句柄的叫法在window上较多,在unix/linux等系统上大多称之为描述符,为了抽象不同平台的差异,libuv ...
- js function集合
/*跳转并是刷新界面*/ function page_back(){ history.go(-); location.reload(); } function show_div(obj){ if(ob ...
- java 多线程 1 线程 进程
Java多线程(一).多线程的基本概念和使用 2012-09-10 16:06 5108人阅读 评论(0) 收藏 举报 分类: javaSE综合知识点(14) 版权声明:本文为博主原创文章,未经博 ...
- Android中AIDL的理解与使用(一)——跨应用启动/绑定Service
AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...
- Oracle 查询语句(where,order by ,like,in,distinct)
select * from production;alter table production add productionprice number(7,2); UPDATE production s ...
- lcov和gcov的使用错误
编译使用的gcc版本和gcov的版本对不上的话,使用lcov和gcov的时候会报错 lcov的错误: [xx@localhost XXX]$ lcov --capture --directory co ...
- JavaScript面向对象和原型函数
对象,是javascript中非常重要的一个梗,是否能透彻的理解它直接关系到你对整个javascript体系的基础理解,说白了,javascript就是一群对象在搅..(哔!). 常用的几种对象创建模 ...
- 使用jstack分析cpu消耗过高的问题
我们使用jdk自带的jstack来分析.当linux出现cpu被java程序消耗过高时,以下过程说不定可以帮上你的忙: 1.top查找出哪个进程消耗的cpu高 21125 co_ad2 18 ...
- linux 下安装 mysql5.7.16安装
1.groupadd mysql ## 添加一个mysql组 2.useradd -r -g mysql mysql ## 添加一个用户 3.解压缩下载的包,tar -xzvf /da ...
- docker 目录迁移
(ubuntu 12.04) 默认路径 /var/lib/docker --> /data/docker root@node1:~# service docker stop root@node ...