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. 配置ogg目录索引-oracle与mysql的双向同步步骤

    以下几篇文章描述了利用ogg对oracle与mysql进行双向同步的配置过程以及注意事项,欢迎参考. 配置ogg异构oracle-mysql(1)基础环境配置 http://www.cnblogs.c ...

  2. 高效jQuery的奥秘

    讨论jQuery和javascript性能的文章并不罕见.然而,本文我计划总结一些速度方面的技巧和我本人的一些建议,来提升你的jQuery和javascript代码.好的代码会带来速度的提升.快速渲染 ...

  3. merge布局

    当LayoutInflater遇到这个标签时,它会跳过它,并将<merge />内的元素添加到<merge />的父元素里.迷惑了吗?让我们用<merge />来替 ...

  4. Redis持久化实践及灾难恢复模拟

    参考资料: Redis Persistence http://redis.io/topics/persistence Google Groups https://groups.google.com/f ...

  5. SpringMVC解析4-DispatcherServlet逻辑脉络

    HttpServlet提供了不同的服务方法,它们是doDelete(),doGet(),doOptions(),doPost(),doPut(),和doTrace(),它会根据不同的请求形式将程序引导 ...

  6. SpringMVC解析2-ContextLoaderListener

    对于SpringMVC功能实现的分析,我们首先从web.xml开始,在web.xml文件中我们首先配置的就是ContextLoaderListener,那么它所提供了功能有哪些又是如何实现的?当使用编 ...

  7. Asp.net mvc 有关序列化的问题

    //            $.ajax({//                async: false,//                type:'post',//这里注意,Get请求不安全,用 ...

  8. Codeforces Round #338 (Div. 2)

    水 A- Bulbs #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1 ...

  9. 给Nginx配置一个自签名的SSL证书

    转自廖雪峰的官方网站http://www.liaoxuefeng.com/ 要保证Web浏览器到服务器的安全连接,HTTPS几乎是唯一选择.HTTPS其实就是HTTP over SSL,也就是让HTT ...

  10. C# 字符串格式化

    1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...