[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)
上一篇 2022年1月24日 上午9:00
下一篇 2022年1月24日 上午9:00


相关推荐

  • pstack 与 strace[通俗易懂]

    pstack 与 strace[通俗易懂]strace  strace是可以记录程序系统调用的一个工具,它可以记录该程序系统调用的各个延时情况。-c统计每一系统调用的所执行的时间,次数和出错的次数等.-d输出strace关于标准错误的调试信息.-f跟踪由fork调用所产生的子进程.-ff如果提供-ofilename,则所有进程的跟踪结果输出到相应的filename.pid中,pid是各进程的进程号.-F尝试跟踪vf…

    2025年11月17日
    6
  • 工信部专家提示 “龙虾”更新后依然存在安全风险 部分openclaw概念股继续回调 迅策(03317)跌近6%

    工信部专家提示 “龙虾”更新后依然存在安全风险 部分openclaw概念股继续回调 迅策(03317)跌近6%

    2026年3月13日
    2
  • mac idea 2021 激活【中文破解版】

    (mac idea 2021 激活)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~4M7H…

    2022年3月29日
    53
  • 刷屏神器QQ(刷屏神器定海神针复制)

    在电脑上桌面新建一个txt,然后在里面写入下面的代码,写好后把文件的txt格式改为vbs格式即可次数别设太多,会卡死,间隔时间别太短setwshshell=wscript.createobject(“wscript.shell”)wshshell.AppActivate”王择玉”fori=1to2wscript.sleep100wshshell.sendKeys…

    2022年4月16日
    316
  • excel变成xml格式_XML文件

    excel变成xml格式_XML文件最近做了一个小工具,可以将XML和Excel之前互转。里面用到的XML读写库是tinyxml,在Excel2010上运行,请先确保装了Excel,而不是WPS。代码写的比较挫,一大坨,最近忙也懒得去做优化了。github地址:XML与Excel互转工具目前只支持ANSI格式的Excel文件与ANSI格式的XML文件互转。由于在写的时候,里面的存储方式都是CString,默认为ANS

    2022年8月22日
    8
  • vector>初始化_vector如何重置

    vector>初始化_vector如何重置vector<int>a(nums.begin(),nums.end());注意:vector<int>a(nums.begin()+1,nums.end()-2);中a包含nums.begin()+1,不包含nums.end()-2,也就是包含的区间是左闭右开区间

    2025年11月26日
    5

发表回复

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

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