[LeetCode][Java] 3Sum Closest
题目:
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的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- 【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 ...
- 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 ...
- leetcode 16. 3Sum Closest JAVA
题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...
- LeetCode (13): 3Sum Closest
https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...
- 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 ...
随机推荐
- ftp上传错误
明明设置好了权限,但是在上传的时候提示如下错误,但在使用的过程当中,发现有的时候是可以上传的,很奇怪的问题. baidu 了一下,发现是下面的这个设置导致的.改过来后,果然正常. 这个设置只是一个字符 ...
- IAR之工程配置
参考 : IAR的Workspace顶部下拉菜单中Debug和Release http://blog.csdn.net/yanpingsz/article/details/5588525 ++++++ ...
- Android 中 ListView Adapter getView 被多次调用问题 解决方法
执行多次原因是因为每显示一个VIew,它都去测量view的高度,执行measure方法,导致getView执行多次. 解决方法是将 ListView 的 layout_width 设置为 fill_p ...
- 使用jquery 1.7 及以后的版本 attr 问题
跟进jquery的代码进行检查,发现问题出在下面的代码中: if ( notxml ) { name = name.toLowerCase(); hooks = jQuery.attrHooks[ n ...
- Week8(10月31日):并发
Part I:提问 =========================== 1. 更新关联.删除关联数据,需要注意哪些问题?以Instructor类为例说明. 2. 已知某请假系统,请实现以下界面的 ...
- grid.Column INT 所对应的文本
grid.Column("RoleId", "角色名称", (p) => { var role = string.Empty; if (p.RoleId ...
- Delphi_MemoryModule — load DLL from memory. Also includes hooking utilities.
https://github.com/Fr0sT-Brutal/Delphi_MemoryModule
- 多层TreeWidget可选实现
http://download.csdn.net/detail/lingyunfuyu2/9117481
- VC++在对话框中加入属性页
当一个基于对话框的程序中有相当多的控件时,你一定会想到使用属性页来将这些控件分类放置.本文针对这种方法来讨论几种可能实现的方案. 方案一本方案的例子请见源代码打包文件中的Property1部分 在对话 ...
- UVA 10763 Foreign Exchange 出国交换 pair+map
题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...