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. 用.NET开发通用Windows App

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:随着Windows 10的正式发布,作为.NET开发人员应该开始或多或少了解一下通用( ...

  2. Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (转)

    Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. jar和源码下载地址: h ...

  3. 关于html中的设置body宽高的理解

    有时候看到别人的代码中经常出现在body中设置的宽高,不是很理解,于是自己测试了下,瞬间懂了,废话不多说,直接上代码: 首先创建好一个基本的html文件,设body的背景色为red: 相信大家都知道效 ...

  4. Android的Touch事件处理机制

    Android的Touch事件处理机制比较复杂,特别是在考虑了多点触摸以及事件拦截之后. Android的Touch事件处理分3个层面:Activity层,ViewGroup层,View层. 首先说一 ...

  5. js实现页面的自动读秒跳转

    <!-- 代码片段A --> <!-- 倒计时跳转 --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Trans ...

  6. vim使用01

    安装与基础配置 iTerm快捷操作 清屏: <C l>/<W k> 剪切: <W x> 复制: <W v> 新增窗口: <W d> 切换窗口 ...

  7. spring优化使用

    1.bean由框架填充,避免手写优化代码. 2.view的展示通过配置或注解实现最优化使用架构. 待续...

  8. Maven的简单使用,HelloWorld

    安装好Maven后就用一个简单的HelloWorld程序来测试一下,体验一下Maven.至于不懂的地方,请查看<Maven实战>书籍. 书籍网址:http://download.csdn. ...

  9. git 学习笔记3--status flow

    1.status 通过执行 git status 命令,查看输出的信息来理解文件所处的状态以及可能的动作. 1.1 nothing to commit (working directory clean ...

  10. POJ3084 Panic Room(最小割)

    把某点与某几点分开的最小花费,当然想到最小割.具体怎么建图,可以画个简单的情况,然后就清楚了: 0到1不受控制,建立0->1容量为INF的边: 1到0受在0一边的一个控制面板的控制,建立1-&g ...