[Leetcode] 3Sum Closest

Source: Internet
Author: User

3Sum Closest

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).

Ideas:

The first idea is to sort, so that it is convenient for later pruning. Although the final proof of sorting and pruning yarn relationship is not, but it is this step intuition let me do this problem.

After sorting, it is three numbers how to choose. The first one is very good, that is to iterate over the container, so the first number constitutes the first layer of the loop. The second and third numbers can also be selected in order, three numbers and from the smallest to the maximum, but the more intuitive method is to take the smallest, one takes the largest, so that the three number and is located between the minimum and maximum (the first number is fixed, changing the value of the second and third numbers). As an example, [1,2,3,4,5]. A1=1, a2=2, a3=5, these three numbers and 8, are located between 1+2+3=6 (min) and 1+4+5=10. The purpose of this is to facilitate the logical organization behind it, making logic more intuitive.

Compare three numbers with the target, smaller than the target, and the left plus 1 to increase the next three numbers, larger than the target, and the right minus 1 is the next three numbers and decreases. This constitutes the second cycle.

Exercises

classSolution { Public:    intThreesumclosest (vector<int> &num,inttarget)        {Sort (Num.begin (), Num.end ()); intsum = num[0]+num[1]+num[2]; intDIF = ABS (sum-target); intres =sum;  for(intI=0; I<num.size ()-2; i++) {            intL = i+1; intR = num.size ()-1;  while(l<r) {sum= num[i]+num[l]+Num[r]; if(sum==target)returnsum; if(sum>target) R--; Else if(sum<target) L++; if(ABS (Sum-target) <dif) {Res=sum; DIF= ABS (sum-target); }            }        }        returnRes; }};
View Code

[Leetcode] 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.