167. Both Sum ii-input array is sorted

Source: Internet
Author: User

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.

Note:

    • Your returned answers (both Index1 and INDEX2) is not zero-based.
    • You may assume this each input would has exactly one solution and you may not use the same element TWIC E.

Example:

Input:numbers = [2,7,11,15], target = 9Output: [1,2]explanation:the sum of 2 and 7 is 9. Therefore index1 = 1, Index2 = 2.

This problem just do not know when the topic let me return what ... an array of size returnsize, the return value is a pointer to an int array of length returnsize.

1 /**2 * Return An array of size *returnsize.3 * Note:the returned array must is malloced, assume caller calls free ().4 */5 int* Twosum (int* Numbers,intNumberssize,intTargetint*returnsize) {6     intmid,low=0, high=numberssize-1, sum;7     int*nums= (int*)malloc(sizeof(int)*2); 8      while(low<=High ) {9sum=numbers[low]+Numbers[high];Ten         if(sum==target) { Onenums[0]=low+1; Anums[1]=high+1; -              Break; -         }     the         Else if(Sum<target) low++; -         Elsehigh--; -     } -*returnsize=2; +     returnnums; -}

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.