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

算法:固定第一个,另外两个数适用双指针。O(N^2)

java:

public class Solution {
public int threeSumClosest(int[] num, int target) {
int len=num.length; int distance=Integer.MAX_VALUE;
int sum=0;
Arrays.sort(num); for(int i=0;i<len-2;i++){
int s=i+1;
int e=len-1;
while(s<e){
int cnt= num[i]+num[s]+num[e];
int dis = Math.abs(target-cnt); if(dis<=distance){
sum=cnt;
distance=dis;
if(sum==target){
return sum;
}else if(sum<target){
s++;
}else {
e--;
}
}else if(cnt<target){
s++;
}else{
e--;
}
}
}
return sum;
}
}

c++:

class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int size=num.size();
sort(num.begin(),num.end());
int close=0;
int dis=INT_MAX;
for(int i=0;i<size-2;i++){
int j=i+1;
int k=size-1;
while(j<k){
int sum=num[i]+num[j]+num[k];
if(abs(sum-target)<=dis){
dis=abs(sum-target);
close=sum;
if(sum==target){
return close;
}else if(sum<target){
j++;
}else{
k--;
}
}else if(sum-target<0){
j++;
}else{
k--;
}
}
}
return close;
}
};

3Sum Closest的更多相关文章

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

  2. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

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

  3. No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  4. 【leetcode】3Sum Closest

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

  5. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  6. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  7. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  8. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. LeetCode--No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  10. leetcode-algorithms-16 3Sum Closest

    leetcode-algorithms-16 3Sum Closest Given an array nums of n integers and an integer target, find th ...

随机推荐

  1. 笔记本电脑关闭小键盘(即打字按P出现星号键)

    开关方法:Fn + NumLk (联想电脑的NumLk 一般为F8,其他电脑自己在键盘找找罗)

  2. 在Salesforce中可以对某一个Object的Standard Button或Link进行重写

    在Salesforce中可以对某一个Object的Standard Button或Link进行重写,来实现我们特定的逻辑过程,比如:在删除某个Object之前要判断该Object的某个Field的状态 ...

  3. html table之 全选,全不选

    就是这个小功能让我和组长引发争端,就是这个小功能让我差点"被"辞职,就是这个自封装的js方法让我放下了对组长的敬畏之心,现在分享一下,其实也很简单,但是真的有这么简单吗? < ...

  4. 在MySQL中存储大文件

    我们的目标:把一首mp3保存到MySQL数据库中! 由于MySQL默认当存入的数据太大时会抛异常,所以应在my.ini中添加如下配置!max_allowed_packet=10485760,这样,可以 ...

  5. Laravel错误与日志处理

    App\Exceptions\Handler class is where all exceptions triggered by your application are logged and th ...

  6. 到程序集里取DLL

    C:\Windows\assembly\gac_msil

  7. 《DSP using MATLAB》 示例Example4.1

    今天开始看第4章,从开始看这本书到现在,过去一个多月,收获不少,继续坚持.

  8. 农资产品送货车上使用 PDA手持机 现场销售开单 然后开单后能直接通过移动网络传回电脑(云服务器)

    客户挑战与需求 目前很多中小型企业或零售商店都会使用各种进销存管理软件以提高管理效率,但是由于这类软件都是安装在PC端,更多的是帮助客户通过进销存的管理分析企业营运账款,并不能实时地反映当前销售.库存 ...

  9. VS2013单元测试及代码覆盖率分析--Xunit

    1,Javaweb中有jmeter.jacoco.ant.badboy等集成测试代码覆盖率的方式,C#代码的覆盖率怎么测试呢?VS2013的IDE上本身并未集成测试的工具,以下讲解VS2013中C#代 ...

  10. hive streaming 使用shell脚本

    一.HIVE streaming 在Hive中,需要实现Hive中的函数无法实现的功能时,就可以用Streaming来实现.其原理可以理解成:用HQL语句之外的语言,如Python.Shell来实现这 ...