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. Mysql添加外键约束

    简单说一下使用外键的好处 1.完整性约束 比如:用户表中有字段 用户编号(id) , 名称(username)设备表中有字段 设备编号(id) , 设备名称(devicename) 设备属于的用户编号 ...

  2. [Liferay6.2]AUI表单验证示例

    <%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib uri="http://jav ...

  3. zoj 3471(状态压缩)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 dp[state]表示当前状态为state时的所能获得的最大值 ...

  4. FFmpeg与libx264 x264接口源代码简单分析

    源代码位于“libavcodec/libx264.c”中.正是有了这部分代码,使得FFmpeg可以调用libx264编码H.264视频.  从图中可以看出,libx264对应的AVCodec结构体ff ...

  5. hdu1166 线段树

    Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...

  6. JS操作select下拉框动态变动(创建/删除/获取)

    1.动态创建select function createSelect(){ var mySelect = document.createElement_x("select"); m ...

  7. 数据库查询Database中的表

    public class UserDA { SqlConnection conn; SqlCommand cmd; public UserDA(Use uuu) { conn =new SqlConn ...

  8. js html 一些技巧

    0.同步或异步ajax进行提交add update delete等. 在ajax中传参数可以和web端bean对应 1.ajax完成后跳转可以在js中使用 window.location.href=u ...

  9. PHP 不使用第三个变量实现交换两个变量的值

    //字符串版本 结合使用substr,strlen两个方法实现$a="a";$b="b";echo '交换前 $a:'.$a.',$b:'.$b.'<br ...

  10. 我的c++学习(1)hello world!

    // texthello.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using na ...