[Leetcode] [013] The Sum 2

Source: Internet
Author: User

Topic:

Given an array of integers that's already sorted in ascending order, find, numbers such, they add up to a specific Target number.

The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.

You may assume this each input would has exactly one solution.

Input:numbers={2, 7, one, A, target=9
Output:index1=1, index2=2

Test instructions: Please first look at "[leetcode][012] two Sum 1", we found out what similarities and differences did not (not found the child upper wall), this topic only changed the input criteria, input array is an array of sorted, and is rendered in ascending order. The input conditions are strict, and we can still use the method from the previous one to ask the husband. Now that this is ascending, can we not use the previous method and be able to achieve O (n) time complexity? The answer is inevitable.

Key point:

Since the array is sorted, the size relationship before the element is relatively certain, the existing pair of elements must be in a relatively fixed position, we can use two cursors, one from the head, one from the tail, the sum of the elements of the two, if large, adjust the back of the cursor, instead, adjust the previous cursor.

Keng:

Since the sum of the two int is likely to occur int_max, if the input type is given an int, then we need to use a long long type to represent the situation in order to avoid overflow.

Answer:

The following is the C + + implementation code:

1 classsolution{2  Public:3std::vector<int> Twosum (std::vector<int> &numbers,inttarget) {4std::vector<int>Vecret;5         intNleft =0;6         intNright = Numbers.size ()-1;7 8          while(Nleft <nright) {9             //beware of the sum overflow of two int, using a long long typeTen             Long Long intNadd = Numbers[nleft] +Numbers[nright]; One             if(Nadd = =target) { AVecret.push_back (Nleft +1); -Vecret.push_back (Nright +1); -  the                 returnVecret; -             } -             Else if(Nadd >target) { -nright--; +             } -             Else if(Nadd <target) { +nleft++; A             } at         } -  -         returnVecret; -     }  -};

Operation Result:

  

I hope you crossing to enlighten me, little brother Thanksgiving words thanks.

[Leetcode] [013] The Sum 2

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.