[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 ...
随机推荐
- win8系统特别慢的基本判断方法
单位有6,7台同事的电脑是win8.1.不知道什么原因,两位领导反映电脑特别慢. 我查看了一下,确实很慢. 虽然知道win8系统不好,但也不至于这样. 打开任务管理器,看了一下cpu和内存的使用状态, ...
- iOS 常用开源代码整理
本文章不定期整理. 1.AFNetworking AFNetworking 采用 NSURLConnection + NSOperation, 主要方便与服务端 API 进行数据交换, 操作简单, 功 ...
- Google Code Jam Round 1C 2015 Problem A. Brattleship
Problem You're about to play a simplified "battleship" game with your little brother. The ...
- Flask web开发 处理POST请求(登录案例)
本文我们以一个登录例子来说明Flask对 post请求的处理机制. 1.创建应用目录,如 mkdir example cd example 2.在应用目录下创建 run.py文件,内容如下 fr ...
- 安装Tomcat指定JDK(转)
一.应用实例 一般情况下一台服务器只跑一个业务,那么就直接配置一套环境,设置好Java环境变量即可.某些时候一台服务器上会安装多个业务,而且各个业务需要的JDK版本各不相同,或者为了使业务独立开来,需 ...
- VC多线程编程
一.问题的提出 编写一个耗时的单线程程序: 新建一个基于对话框的应用程序SingleThread,在主对话框IDD_SINGLETHREAD_DIALOG添加一个按钮,ID为IDC_SLEEP_SIX ...
- 零成本建立的.NET小组开发平台
前言 说道.NET开发平台,首先想到的就是Visual Studio,建立.NET小组开发平台自然首推TFS.但其花费却也是相当昂贵的(当然在本国可以无视这些成本),近期的开发中接触到一些开源软件并读 ...
- C#验证码的另一种操作方法
sb = new StringBuilder(); char c = '0'; string s = ""; for (int i = 0; i < 4; i++) { Ra ...
- bootloader启动代码init.s解析----IRQ中断处理函数
bootloader启动代码init.s解析----IRQ中断处理函数 init.s源代码如下: ;///////////////////////////////////////////// ;opt ...
- BZOJ 1578: [Usaco2009 Feb]Stock Market 股票市场( 背包dp )
我们假设每天买完第二天就卖掉( 不卖出也可以看作是卖出后再买入 ), 这样就是变成了一个完全背包问题了, 股票价格为体积, 第二天的股票价格 - 今天股票价格为价值.... 然后就一天一天dp... ...