[Leetcode] [Java] 3Sum Closest

Source: Internet
Author: User

Title:

Given an array S of n integers, find three integers in S such so the sum is closest to a give n number, target. Return the sum of the three integers. You may assume this each input would has exactly one solution.

    For example, given array S = {-1 2 1-4}, and target = 1.    The sum is closest to the target is 2. (-1 + 2 + 1 = 2).

Test Instructions:

Given an array of n integers, find three integers in the array so that the three integers are closest to the target value. Returns the and of the three integers. You can assume that for each integer, there is a definite solution.

    For example, given array S = {-1 2 1-4}, and target = 1.    The sum is closest to the target is 2. (-1 + 2 + 1 = 2).

Algorithm Analysis:


Reference Blog: http://www.zhuangjingyang.com/leetcode-3sum/

      and 3Sum. But here we want to judge the condition is not in three numbers and for 0 but for a number closer to the target.

      We still use the 3Sum algorithm, if there are three numbers x1 + x2 + x3 = Result what we ask is to get the result closest to target. So for num, sort first, and then traverse each number whose subscript is greater than its own two digits. Then set two global variables a minval is used to record its distance from Target, and the new value of result is updated when the distance decreases.


AC Code:

<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 was equal to index+1 just Bec    Ause We just search element 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>


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] 3Sum Closest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.