[LeetCode][Java] 3Sum Closest「建议收藏」

[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>



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116237.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python数组截取

    python数组截取importtensorflowastfimportnumpyasnp#one-dimentiona=np.array([0,1,2,3,4,5,6,7,8,9])print(a)b=a[:4]print(b)c=a[4:]print(c)#multi-dimentiond=np.array([[0,1,2,3,4,5,6,7,8,9],[1,2,3,4…

    2022年4月30日
    49
  • 1. C语言—输出格式大全[通俗易懂]

    1. C语言—输出格式大全[通俗易懂]1.C语言—输出格式大全#include<stdio.h>intmain(){ //1.整形输出 inta=298;//输出十进制用%d printf(“1.十进制: a=%d\n”,a);//只能采用英文双引号格式,不能用单引号 //2.输出八进制用%o printf(“2.八进制:a=%o\n”,a); //3.使用%#o,可以输出八进制数的前导符(在八进制数前面加一个0,用来标识八进制数), printf(“3.前导

    2022年7月24日
    12
  • 超详细VSCode安装教程(Windows)「建议收藏」

    本文基于Windows系统博主的VSCode专栏:分享使用VSCode的基本操作与各种技巧

    2022年4月4日
    150
  • python激活码 3月最新注册码

    python激活码 3月最新注册码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月15日
    98
  • 差分数组C++「建议收藏」

    差分数组C++「建议收藏」差分数组在数组某一段数值同乘以一个值,或者求某数组的前n项和非常方便inta[]={0,1,2,3,4,5};b[i]=a[i]-a[i-1];(1<i≤n,b[1]=a[1],a[0]=b[0]=0)b[]={0,1,1,1,1,1}则称b[]是a的差分数组,它具有的性质是a[i]=b[i]+b[i-1]+…+b[1];如果要在数…

    2022年6月6日
    42
  • 命令行查看cuda版本_ubuntu查看cudnn版本

    命令行查看cuda版本_ubuntu查看cudnn版本linux查看cuda版本1.cat/usr/local/cuda/version.json2.或者nvcc-V(注意是大写)3nvidia-smi1和3中显示的是11.32中显示的是10.1CUDA有两个主要的API:runtime(运行时)API和driverAPI。这两个API都有对应的CUDA版本(如10.1和11.3等)。用于支持driverAPI的必要文件(如libcuda.so)是由GPUdriverinstaller安装的nvidia-smi就属于

    2022年8月30日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号