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