[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 ...
随机推荐
- JavaScript 高级程序设计(第3版)笔记——chapter4:变量、作用域和内存问题
Chapter4 变量.作用域和内存问题 l 理解基本类型和引用类型的值 l 理解执行环境 l 理解垃圾收集 4.1基本类型和引用类型的值 l ECMAScript变量包含两种不同数据类型的值 ...
- android动画效果演示
第一种:TranslateAnimation 动画效果演示: public void move(View view) { // 传统动画效果 TranslateAnimation animation ...
- 用AS3清空容器下所有子显示对象
容器中的子显示对象分为两类: 处于显示列表中的子显示对象.被numChildren所记录的. 由容器graphics对象绘制出来的矢量图.这个矢量图不属于Shape类型,不在容器的显示列表中,不被nu ...
- C# 方法的调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- reStructuredText(.rst)语法规则快速入门
原文:http://blog.useasp.net/archive/2014/09/05/rst-file-restructuredtext-markup-syntax-quikstart.aspx? ...
- php 学习笔记 数组2
10.切割数组 array_slice(array, offset, length);返回一个由原始数组中的连续元素组成的新数组,参数1为原始数组,参数2为要复制的起始位置, 参数3要复制的个数:新数 ...
- Net FLow Template
EK Template : bool bfs(int src, int des){ memset(pre, -, sizeof(pre)); while(!que.empty()) que.pop( ...
- Fruit Ninja(树状数组+思维)
Fruit Ninja Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 3033 分组背包
给出N个物品.M金钱.W种类 给出N个物品的性质:所属种类,花费.价值 求每一种类物品至少一个的前提下,所能购买到的最大价值 dp[i][k]表示在第i种物品.总花费为k的最大价值 dp[i][k]= ...
- easyui只选择年份月份的插件---SimpleCanleder
<td>日期</td> <td> <input type="text" id="search_begindate" s ...