"Leetcode" 167. Both Sum ii-input array is sorted

Source: Internet
Author: User

Both Sum ii-input array is sorted

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, target=9
Output: index1=1, index2=2

A typical double-pointer problem.

Initializes the left pointer to the start of the array and initializes the right pointer to the end of the array.

Based on the ordered feature,

(1) If the and TMP of Numbers[left] and Numbers[right] are less than target, it indicates that TMP should be increased, so left right moves to a larger value.

(2) If the TMP is greater than target, the TMP should be reduced, so right moves to a lower value.

(3) TMP equals target, then found, returns LEFT+1 and right+1. (Note 1 is the starting subscript)

Time complexity O (n): Sweep again

Space complexity O (1)

PS: Strictly speaking, the addition of two int may overflow int, so the TMP and Target are promoted to a long long int and then the comparison is more robust.

classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) {Vector<int> Ret (2,-1); intleft =0; intright = Numbers.size ()-1;  while(Left <Right ) {            intTMP = numbers[left]+Numbers[right]; if(TMP = =target) {ret[0] = left+1; ret[1] = right+1; returnret; }            Else if(TMP <target)//Make tmp largerLeft + +; Else            //Make tmp smallerRight--; }    }};

Here are my test cases, all tested by:

intMain () {solution S; inta[4] = {2,7, One, the}; Vector<int> numbers1 (A, A +4); Vector<int> ret = s.twosum (numbers1,9); cout<< ret[0] <<", "<< ret[1] <<Endl; intb[2] = {1,1}; Vector<int> numbers2 (b, + +2); RET= S.twosum (Numbers2,2); cout<< ret[0] <<", "<< ret[1] <<Endl; intc[3] = {1,2,3}; Vector<int> Numbers3 (C, c+3); RET= S.twosum (Numbers3,5); cout<< ret[0] <<", "<< ret[1] <<Endl; return 0;}

"Leetcode" 167. Both Sum ii-input array is sorted

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.