3Sum Closest

来源:互联网 发布:淘宝经典差评 编辑:程序博客网 时间:2024/06/10 12:16

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

这道题是3sum的变形,是找出与给出的目标值最接近的那三个数的和

3sum那道题是变形成了两个数的和等于一个定值,然后首尾有两个指针,而这道题也可以变形成两个数的和最接近一个目标值,不过为了更方便理解,我们可以使用三个指针

引用他人的解析:

Sort the vector and then no need to run O(N^3) algorithm as each index has a direction to move.

The code starts from this formation.

----------------------------------------------------^  ^                                               ^|  |                                               ||  +- second                                     third+-first

if nums[first] + nums[second] + nums[third] is smaller than the target, we know we have to increase the sum. so only choice is moving the second index forward.

----------------------------------------------------^    ^                                             ^|    |                                             ||    +- second                                   third+-first

if the sum is bigger than the target, we know that we need to reduce thesum. so only choice is moving 'third' to backward. of course if thesum equals to target, we can immediately return the sum.

----------------------------------------------------^    ^                                          ^|    |                                          ||    +- second                                third+-first

when second and third cross, the round is done so start next round by moving 'first' and resettingsecond and third.

----------------------------------------------------  ^    ^                                           ^  |    |                                           |  |    +- second                                 third  +-first

while doing this, collect the closest sum of each stage by calculating and comparing delta. Compareabs(target-newSum) and abs(target-closest). At the end of the process the three indexes will eventually be gathered at the end of the array.

----------------------------------------------------                                         ^    ^    ^                                         |    |    `- third                                         |    +- second                                         +-first

if no exactly matching sum has been found so far, the value in closest will be the answer.

代码如下:

  class Solution {  
    public:  
        int  threeSumClosest(vector<int>& nums,int target) {  
            if(nums.size()<3)return 0;
            std::sort(nums.begin(),nums.end());  
             int frist,second,back,sum;
             int ret=nums[0]+nums[1]+nums[2];
           for(int i=0;i<nums.size()-2;i++)
           {
               frist=i;
               second=i+1;
               back=nums.size()-1;
               while(second<back)
               {
                   sum=nums[frist]+nums[second]+nums[back];
                   if(abs(sum-target)<abs(ret-target))ret=sum;
                   if(sum<target)second++;
                   else if(sum>target)back--;
                   else return target;
               }
           }
           return ret;
      
        }  
    };