题目:

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

题意:

给定一个包括n个整数的数组S,在数组中找出三个整数。使得这三个整数的和与目标值最为接近。

返回这三个整数的和。你能够假定对于每一个整数。都有确定的一个解。

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

算法分析:

參考博客:http://www.zhuangjingyang.com/leetcode-3sum/

    和3Sum异曲同工。 只是这里我们要推断的条件不在是三个数字和为0而是和为一个更加接近target的数字。

    我们依旧採用3Sum的算法,若有三个数字x1 + x2 + x3 = result 我们所求的便是让result最接近target。

    因此对于num,首先排序。然后遍历每一个数字其下标大于自身的两个数字。然后设置两个全局变量 一个 minVal 用于记录其与target的距离,当距离减小时便更新result的新值。

AC代码:

<span style="font-size:12px;">public class Solution
{
private int minVal = Integer.MAX_VALUE;
private int result = 0;
public int threeSumClosest(int[] num, int target)
{
Arrays.sort(num);
//if number is less than 3 or num is null it's can't be calc
if(num.length <3 || num ==null)
return target;
for(int i=0;i<num.length;i++)
{
if(i>0 && num[i] == num[i-1])
continue;
find(i,num,num[i],target);
}
return result;
}
public void find(int index,int[] num,int target,int res)
{
int l = index+1; //low is equal to index+1 just because we just search element that is bigger than itself
int r = num.length - 1;
while(l<r)
{
if( Math.abs(num[l] + num[r] + target - res) <= minVal)
{
minVal = Math.abs(num[l] + num[r] + target - res);//it's more closer
result = num[l] + num[r] + target;
}
if(num[l] + num[r] + target >res)
r--;
else
l++;
}
}
}</span>

[LeetCode][Java] 3Sum Closest的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  3. 【leetcode】3Sum Closest

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

  4. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  5. 【JAVA、C++】LeetCode 016 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  6. Java [leetcode 16] 3Sum Closest

    题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...

  7. leetcode 16. 3Sum Closest JAVA

    题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...

  8. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

  9. LeetCode 16. 3Sum Closest. (最接近的三数之和)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

随机推荐

  1. asp.net利用剪切板导出excel

    public enum ClipboardFormats : uint { CF_TEXT = 1, CF_BITMAP = 2, CF_METAFILEPICT = 3, CF_SYLK = 4, ...

  2. 数矩形(N - 暴力求解、打表)

    数矩形 Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格.            Input 第一行输入一个t, 表示有t组数据,然后 ...

  3. js window.onload事件

    1.最简单的调用方式 直接写到html的body标签里面,如: ? 1 2 3 4     <html>       <body onload="func()"& ...

  4. 数据结构C语言版 表插入排序 静态表

    数据结构C语言版 表插入排序.txt两个人吵架,先说对不起的人,并不是认输了,并不是原谅了.他只是比对方更珍惜这份感情./*  数据结构C语言版 表插入排序  算法10.3 P267-P270  编译 ...

  5. 完整的yuicompressor单个压缩和批量压缩以及gzip再次压缩,拦截器的配置等

    下载地址:http://yuilibrary.com/download/yuicompressor/ 个人认为现在yuicompressor是最安全,最值得信赖的压缩工具,至少到现在没出现过问题 1. ...

  6. 函数可以作为Javascript对象(哈希表)的键吗

    一般Javascript书在讲解对象时,都指出Javascript中的对象可以作为哈希表,存储键值数据.通常情况下,键为字符串,如果键是数字的话,实际上在内部也会转换为字符串. 比如 var o = ...

  7. http 连接复用

    定义 Http/1.0每次请求都需要建立新的TCP连接,连接不能复用.Http/1.1新的请求可以在上次建立的tcp连接之上发送,连接可以复用. 优点 减少重复进行tcp三次握手的开销,提高效率.注意 ...

  8. jQuery推断复选框是否勾选

    今天要实现一功能就是:复选框勾选时给input表单赋值,复选框取消时将表单值清除. 效果如图: 实现源代码:cyfID为复选框的id $("#cyfID").click(funct ...

  9. C-整数划分

    将正整数 n 表示成一系列正整数之和, n=n1+n2+…+nk, 其中 n1>=n2>=…>=nk>=1 , k>=1 . 正整数 n 的这种表示称为正整数 n 的划分 ...

  10. 02-OC方法、属性

    目录: 一.方法 二.实例变量 三.属性(点语法) 四.初始化方法(自定义构造方法) 回到顶部 一.方法 1 函数与方法有什么区别? 函数只是一个程序的代码段,与类无关. 方法,类的一部分,代表对象可 ...