Leetcode 16. The closest three numbers and _c

Source: Internet
Author: User

Given an array of n integers nums and a target value target. Find the three integers in the nums so that they are closest to target. Returns the and of these three numbers. Assume that there is only one answer for each set of inputs.

For example, given an array nums = [ -1,2,1,-4], and target = 1.

The number of the closest three to target is 2. (-1 + 2 + 1 = 2).

The topic and the previous question Leetcode 15. Three number and the https://blog.csdn.net/u011750466/article/details/80218002 solution mentality is same, the procedure slightly changes.

1. Go to republication

Class Solution {public:int threesumclosest (vector<int>& nums, int target) {sort (Nums.begin (), Nums
        . end ()); The question for int sum=0,close=int_max,m=0,result=0;//conversion to two numbers for (int i=0;i<nums.size () -2;++i) {M=target-nums[i
            ];
                int l=i+1,r=nums.size () -1;//double pointer method while (L&LT;R) {sum=nums[l]+nums[r];
                        if (sum<m) {if (m-sum<close) {close=m-sum;
                    RESULT=NUMS[I]+NUMS[L]+NUMS[R];
                    } ++l;
                    while (L<r&&nums[l]==nums[l-1]) ++l;//to heavy} else if (sum>m) {
                        if (sum-m<close) {close=sum-m;
                    RESULT=NUMS[I]+NUMS[L]+NUMS[R];
                    }--r; while (L<r&&nums[r]==nums[r+1])--r;//to heavy} else retUrn target;//If equality is returned directly} while (I<nums.size () -2&&nums[i]==nums[i+1]) ++i;//to heavy}
    return result; }
};

2. Do not go to republication

Class Solution {public
:
    int threesumclosest (vector<int>& nums, int target) {
        sort (nums.begin () , Nums.end ());
        int sum=0,close=int_max,m=0,result=0;
        for (int i=0;i<nums.size () -2;++i) {
            m=target-nums[i];
            int L=i+1,r=nums.size ()-1;
            while (l<r) {
                sum=nums[l]+nums[r];
                if (sum<m) {
                    if (m-sum<close) {
                        close=m-sum;
                        RESULT=NUMS[I]+NUMS[L]+NUMS[R];
                    }
                    ++l;
                }
                else if (sum>m) {
                    if (sum-m<close) {
                        close=sum-m;
                        RESULT=NUMS[I]+NUMS[L]+NUMS[R];
                    }
                    --r;
                }
                else{return
                    target;
                }
            }
        return result;
    }
;

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.