【leetcode】和为s的两个数字

 

int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int left = 0;
    int right = numsSize - 1;
    *returnSize = 2;
    int *ret = (int*)malloc(*returnSize * sizeof(int));
    while (left < right) {
        if (nums[left] + nums[right] == target) {
            ret[0] = nums[left];
            ret[1] = nums[right];
            return ret;
        } else if (nums[left] + nums[right] > target) {
            right--;
        } else {
            left++;
        }
    }
    return NULL;
}

 

posted @ 2020-08-21 17:35  温暖了寂寞  阅读(87)  评论(0编辑  收藏  举报