"Leetcode-Interview algorithm classic-java Implementation" "016-3 Sum Closest (nearest three numbers)"

Source: Internet
Author: User

"016-3 Sum Closest (nearest three number sum)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

Given an array S of n integers, find three integers in S such, the sum was closest to a Given 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).

Main Topic

Given an array of n integers, find three integers in s, so that it is the closest to a given number. Returns the sum of three integers. You can assume that each input will have an exact resolution.

Thinking of solving problems

and 3sum-like solution, see "013-3 Sum (three numbers)" "

Code Implementation
Import Java.util.Arrays; Public  class solution {     Public intThreesumclosest (int[] Nums,intTarget) {//Record the minimum difference        LongMindiff = Long.max_value;//record three integers corresponding to the minimum difference value and        Longresult =0;//The difference between each of the obtained        LongDiff//Each of the three integers is evaluated and        Long sum;//Sort the array firstArrays.sort (Nums);//I means assuming the number of I as the result         for(inti =0; I < nums.length-2; i++) {//second number possible starting position            intj = i +1;//The third number may be the end position            intK = Nums.length-1; while(J < K) {//For the current three-digit and                sum= Nums[j] + nums[k] + nums[i];difference between current and target anddiff = Math.Abs (Target-sum);///difference is 0 to return directly                if(diff = =0) {return(int)sum; }//If the current difference is smaller than the difference recorded earlier                if(diff < Mindiff) {//Update the minimum differenceMindiff = diff;//update minimum difference corresponding to andresult =sum;//The settings above take effect the next time the element is processed}//and greater than target                if(sum> target) {k--; }//And less than target                Else{j + +; }            }        }return(int) result; }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46980243"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "016-3 Sum Closest (nearest three numbers)"

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.