Two Sum II

32 篇文章 0 订阅
20 篇文章 0 订阅

÷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.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.

  • You may assume that each input would have exactly one solution and you may not use the same element twice.

Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

暴力求解法:

1.循环遍历数组,认定numbers[i]为target的一个元素;

2.从i之后遍历数组,找new_target;

int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    *returnSize = 2;
    int *ans = malloc((*returnSize) * sizeof(int));
    for(int i = 0; i < numbersSize; i++){
        int new_target = target - numbers[i];
        for(int j = i + 1; j < numbersSize; j++){
            if(numbers[j] == new_target){
                ans[0] = i + 1;
                ans[1] = j + 1;
                return ans;
            }
        }
    }
    return 0;
}

÷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.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.

  • You may assume that each input would have exactly one solution and you may not use the same element twice.

Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

暴力求解法:

1.循环遍历数组,认定numbers[i]为target的一个元素;

2.从i之后遍历数组,找new_target;

int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    *returnSize = 2;
    int *ans = malloc((*returnSize) * sizeof(int));
    for(int i = 0; i < numbersSize; i++){
        int new_target = target - numbers[i];
        for(int j = i + 1; j < numbersSize; j++){
            if(numbers[j] == new_target){
                ans[0] = i + 1;
                ans[1] = j + 1;
                return ans;
            }
        }
    }
    return 0;
}

基于杨式矩阵法

时间复杂度O(n)

空间复杂度O(1)

代码量就减少很多啦

这是一种思维模式,把寻找两数之和看成一个二维数组,二维数组中存的数就是目标值target,从左下角向上爬楼梯和从右上角向下走楼梯都可以,以从右上角向下爬楼梯为例,(a[0]{3} = 17)>target,在杨式矩阵中,一个值的左边总是比这个值小,一个值的下面总是比这个值大,所以应该向左走一个格子,走到(a[0]{2} >target),继续向左走,走到(a[0]{1}] = target)找到值,返回。

那么就这到题而言,我们并不需要开辟一个二维数组,我们只需要,把i看成是行,j看成是列

nums[i] + nums[j] > target 在杨式矩阵中向左走,即j--;

nums[i] + nums[j] < target 在杨式矩阵中向下走,即i++;

注意:i = j时,不满足题意,记得舍去哟!!!

在杨式矩阵中找到一个值x可以有两种方法:1.从右上角 2.从左下角

 271115
2491317
79141822
1113182226
1517222630

 

   
     
/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
    *returnSize = 2;
    int *ans = (int *)malloc(sizeof(int) * (*returnSize));
    int i = 0, j = numbersSize - 1;
    while(numbers[i] + numbers[j] != target){
        if(numbers[i] + numbers[j] > target) j--;
        else i++;
    }
    if(i == j) return ans;
    else ans[0] = i + 1, ans[1] = j + 1;

    
    
    return ans;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值