[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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sublime text2 安装及使用教程

    sublime text2 安装及使用教程1.下载安装包地址:https://www.sublimetext.com/22.安装,一直点下一步就好,将下列选项打钩,这样文件右键就可以直接用sublimetext2打开3.新建一个html

    2022年7月1日
    17
  • 北京软件公司排名-中国十大软件外包公司排名[通俗易懂]

    北京软件公司排名-中国十大软件外包公司排名[通俗易懂]北京软件公司排名-中国十大软件外包公司排名

    2022年6月4日
    165
  • pycharm中文语言包下载不了_pycharm基本使用方法

    pycharm中文语言包下载不了_pycharm基本使用方法本文为原文转载PyCharmproforMac(编程开发软件)​www.mac69.com安装完成后运行,进行基本的配置后会弹出激活窗口,选择「Evaluateforfree」,然后点击「Evaluate」按钮。PyCharmproforMac(编程开发软件)安装完成后运行,进行基本的配置后会弹出激活窗口,选择「Evaluateforfree」,然后点击「Evaluate」按钮…

    2022年8月29日
    2
  • Linux 修改文件权限

    Linux 修改文件权限目录:1、介绍:2、权限说明3、用户4、权限设置5、权限设置格式:(1)增加权限:(2)撤销权限(3)无任何权限1、介绍:​ Linux系统中,每个文件或目录都有访问许可权限,用它来确定以何种方式对文件或目录进行访问和操作。在Linux中,如果要对文件的权限进行修改,那么可在终端中使用chmod命令对其文件的权限进行修改,但是chmod命令修改文件权限有两种方式:1、字母法,2、数字法2、权限说明​ (1)只读:表示只允许读取内容,而禁止其对该文件做其他任何操作​ 字母法:‘r’

    2025年10月29日
    3
  • Rabbitmq原理理解[通俗易懂]

    Rabbitmq原理理解[通俗易懂]rabbitmq特点RabbitMQ是一个由Erlang语言开发的AMQP的开源实现。AMQP:AdvancedMessageQueue,高级消息队列协议。可靠性(Reliability)灵活的路由(FlexibleRouting)消息集群(Clustering)高可用(HighlyAvailableQueues)多种协议(Multi-protocol)多语言客户端(ManyClients)管理界面(ManagementUI)跟踪机制(Tracing)插件机

    2022年6月17日
    34
  • pytest-allure_python unittest

    pytest-allure_python unittest前言allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面。mac环境:

    2022年8月6日
    9

发表回复

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

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