leetcode1.两数之和

题目描述

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。

你可以按任意顺序返回答案。

示例 1:

输入:nums = [2,7,11,15], target = 9

输出:[0,1]

解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:

输入:nums = [3,2,4], target = 6

输出:[1,2]

示例 3:

输入:nums = [3,3], target = 6

输出:[0,1]

提示:

2 <= nums.length <= 104

-109 <= nums[i] <= 109

-109 <= target <= 109

只会存在一个有效答案

解题思路

方法一:双指针

c 复制代码
int cmp(const void *a,const void *b){
    return (*(int*)a)-(*(int*)b);
}
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    int* res = (int*)malloc(2 * sizeof(int));
    if (nums == NULL || numsSize == 0) {
        return res;
    }
    int *copy=(int*)malloc(numsSize* sizeof(int));
     for(int i=0;i<numsSize;i++){
        copy[i]=nums[i];
     }
      qsort(copy,numsSize,sizeof(int),cmp);
    int left=0,right=numsSize-1;
    while(left<right){
        int sum=copy[left]+copy[right];
        if(sum==target){
           for(int i=0;i<numsSize;i++){
                if(nums[i]==copy[left]){
                    res[0]=i;
                    break;
                }
            }
            for(int i=0;i<numsSize;i++){
                if(nums[i]==copy[right]&&i!=res[0]){
                    res[1]=i;
                    break;
                }
            }
             *returnSize = 2;
             return res;
            
        }
        else if(sum>target)
        right--;
        else{
           left++;
        }
    }
    *returnSize = 0;
    return NULL;
}

```c
int main() {
    int nums[] = {2, 7, 11, 15};
    int target = 9;
    int returnSize;
    int* result = twoSum(nums, sizeof(nums) / sizeof(nums[0]), target, &returnSize);

    if (returnSize == 2) {
        printf("Indices: %d, %d\n", result[0], result[1]);
    } else {
        printf("No two sum solution\n");
    }

    free(result);
    return 0;
}

方法二:哈希表

c 复制代码
#define size 10000
#define mid 5000
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    int* res = (int*)malloc(2 * sizeof(int));
     *returnSize=2;
    if (nums == NULL || numsSize == 0) {
        return res;
    }
    int* hash=(int*)calloc(size,sizeof(int));
    memset(hash,-1,size*sizeof(int));
    for(int i=0;i<numsSize;i++){
        int other=target-nums[i];
        if(hash[other+mid]!=-1){
            res[0]=hash[other+mid];
            res[1]=i;
       
        return res;
        }
        hash[nums[i]+mid]=i;
    }
    *returnSize=0;
    return NULL;
}
相关推荐
豆豆酱18 分钟前
Informer方法论详解
算法
槐月初叁22 分钟前
多模态推荐系统指标总结
算法
迪小莫学AI38 分钟前
LeetCode 2588: 统计美丽子数组数目
算法·leetcode·职场和发展
昂子的博客1 小时前
热门面试题第十天|Leetcode150. 逆波兰表达式求值 239. 滑动窗口最大值 347.前 K 个高频元素
算法
卑微小文2 小时前
2025国内网络反爬新高度:代理IP智能轮换算法揭秘
后端·算法·架构
辰尘_星启3 小时前
【vscode】一键编译运行c/c++程序
c语言·c++·vscode·debug·cmake
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧3 小时前
C语言_数据结构总结7:顺序队列(循环队列)
c语言·开发语言·数据结构·算法·visualstudio·visual studio
LIUJH12333 小时前
数据结构——单调栈
开发语言·数据结构·c++·算法
2301_807449203 小时前
字符串相乘——力扣
java·算法·leetcode
---yx8989784 小时前
数字人系统源码---v10技术五大底层架构链路全局开发思路
算法·架构·数字人·数字人源码·数字人系统