力扣算法:两数之和

题目地址:1. 两数之和 - 力扣(LeetCode)

题目

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {

这个题,用暴力很容易想出来,但是我脑子一抽,想尝试用二维数组存储每两个数之和,和他们相加的两个数在原数组的下标,哈希表我还没搞明白,感觉他们应该挺像的,ok,让我们看题,他有一个前提,每种输出只会对应一种答案,也就是说,我们设计的二维数组不用考虑相关数相加相等的情况,在哈希表中,应该是哈希冲突。我们通过计算可以得到我们需要的数组的大小(简单的数学问题)

int num = numsSize * (numsSize - 1) / 2;

用mallco分配相应大小即可。 将我们计算二维数组创建出来之后,循环存入所有不重复数相加的数,及其在原数组内的下标

 // 计算两两相加和及其索引,并存储在二维数组中
    int count = 0;
    for(int i = 0; i < numsSize; i++) {
        for(int b = i + 1; b < numsSize; b++) {
            arr[count][0] = nums[i] + nums[b];
            arr[count][1] = i;
            arr[count][2] = b;
            count++;
        }
    }

最后一遍遍历释放内存,如果没有找到,返回空指针

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    // 计算两两相加和的组合数
    int num = numsSize * (numsSize - 1) / 2;

    // 为存储两数和及其索引的二维数组分配内存
    int** arr = (int **)malloc(sizeof(int*) * num);
    if (arr == NULL) {
        return NULL;
    }

    for (int i = 0; i < num; i++) {
        arr[i] = (int*)malloc(sizeof(int) * 3);
        if (arr[i] == NULL) {
            perror("malloc");
            return NULL;
        }
    }

    // 计算两两相加和及其索引,并存储在二维数组中
    int count = 0;
    for(int i = 0; i < numsSize; i++) {
        for(int b = i + 1; b < numsSize; b++) {
            arr[count][0] = nums[i] + nums[b];
            arr[count][1] = i;
            arr[count][2] = b;
            count++;
        }
    }

    // 寻找满足目标和的索引并返回
    for(int i = 0; i < num; i++) {
        if(target == arr[i][0]) {
            // 找到目标和,返回两个数的索引
            int* result = (int*)malloc(sizeof(int) * 2);
            result[0] = arr[i][1];
            result[1] = arr[i][2];

            // 释放内存
            for (int j = 0; j < num; j++) {
                free(arr[j]);
            }
            free(arr);

            *returnSize = 2;
            return result;
        }
    }

    // 如果没有找到满足目标和的索引,释放内存并返回空指针
    for (int i = 0; i < num; i++) {
        free(arr[i]);
    }
    free(arr);

    return NULL;
}

 但是,这个方向内存越界,,但是我还没有学过哈希表,就只能采用暴力一点的方法,但这也是一个思路,下面就是暴力解答的代码,暴力方法就是一遍一遍遍历数组相加,看能不能找到最后的结果,思路很简单

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    for(int i=0;i<numsSize;i++)
    {
        for(int b=i+1;b<numsSize;b++)
        {
            if(nums[i]+nums[b]==target)
            {
                int* result = (int*)malloc(sizeof(int) * 2);
                result[0] = i;
                result[1] = b;

                *returnSize = 2;
                return result;
            }
        }
    }
    return 0;
}

等我看完哈希函数之后,我会更新这篇文章滴!!很快

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值