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. loj 1412(树上最长直径的应用)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1412 思路:好久没写题解了,有点手生,这题从昨天晚上wa到现在终于是过了...思想其实 ...

  2. 智能车学习(七)——按键矩阵的实现

    一.原理说明 就是按键矩阵代码书写的一个说明,就是讲K5到K7先输出高电平,而K1和K4则调成上拉输入,如果检测到K1到K4有一个变为0,说明有按键按下去,立刻进行转换,是的K1到K4设置为输出高电平 ...

  3. UVA136 求第1500个丑数

    枚举大范围数据..暴力检查题目条件 #include <iostream> #include <cstdio> #include <vector> #include ...

  4. ie上如何兼容placeholder

    首先,判断浏览器是否支持placeholder属性: var input = document.createElement('input'); if("placeholder" i ...

  5. flex_宽度补全

    宽度40px,另一个的补全宽度: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  6. hdu 5692 Snacks 线段树+dfs

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  7. asp中 grideview 更新 无法获取值 解决办法

    string str1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim(); 来 ...

  8. 编解码-protobuf

    Google的Protobuf在业界非常流行,很多商业项目选择Protobuf作为编解码框架,Protobuf的优点. (1)在谷歌内部长期使用,产品成熟度高: (2)跨语言,支持多种语言,包括C++ ...

  9. 快速破解哈希密文findmyhash

    快速破解哈希密文findmyhash   Kali Linux提供各种哈希密文破解工具,如hashcat.john.rainbows.不论哪一种,实施破解都不太容易.每种方式都需要花费大量的时间.破解 ...

  10. [xsd学习]xsd元素限定

    限定(restriction)用于为 XML 元素或者属性定义可接受的值 一.xsd中主要限定格式如下: <xs:element name="xxx"><!--元 ...