[LeetCode]167. Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

C代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <assert.h>
#include <stdlib.h>

/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
int left = 0, right = numsSize-1;
int* returnNums = NULL;
int sum = 0;
while(left < right) {
sum = nums[left]+nums[right];
if(sum == target) {
returnNums = (int *)malloc(sizeof(int)*2);
returnNums[0] = left+1;
returnNums[1] = right+1;
*returnSize = 2;
break;
}
else if(sum > target)
right--;
else
left++;
}
return returnNums;
}

/**
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
int i = 0, j = i+1;
int* returnNums = NULL;
for(i=0, j=i+1; i < numsSize-1;) {
if(nums[i] + nums[j] == target) {
returnNums = (int *)malloc(sizeof(int) * 2);
returnNums[0] = i+1;
returnNums[1] = j+1;
*returnSize = 2;
return returnNums;
}
else if(nums[i] + nums[j] < target) {
j++;
if(j == numsSize) {
i++;
j = i+1;
}
}
else {
i++;
j = i+1;
}
}
return returnNums;
}
*/

int main() {
int nums[3] = {2,3,4};
int returnSize = 0;
int* returnNums = twoSum(nums, 3, 6, &returnSize);
assert(returnNums[0] == 1);
assert(returnNums[1] == 3);

return 0;
}