C实现 LeetCode-> TwoSum

本文介绍了一个算法,用于在给定的整数数组中找出两个数,它们相加等于特定的目标数,并返回这两个数的索引。算法通过排序数组、使用二分查找和快速排序来实现高效搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given an array of integers, 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

给定一个整数数组,找到两个数,使得它们的和等于一个特定的目标数。
函数twoSum应返回两个数的索引(index,从1开始),使它们加起来等于目标,其中索引1 值必须小于索引2 值。



struct IndexValue{
    int  val;
    int  index;
};

struct IndexValue *
create(int  *nums,int nsize){
    int i;
//    struct IndexValue *data = (struct IndexValue *)malloc(sizeof(struct IndexValue*) * nsize);
    struct IndexValue *data = (struct IndexValue *)malloc(sizeof(data) * nsize);
    for (i=0; i<nsize; i++) {
        data[i].index = i;
        data[i].val   = nums[i];
    }
    return data;
}


 //快速排序
static int
cmp(const struct IndexValue*a,struct IndexValue*b){
    return (a->val)- (b->val);
}


void
quick_sort(struct IndexValue *nums,int nsize){
    qsort(nums, nsize, sizeof(struct IndexValue *), (int(*)(const void *,const void *))cmp);
}


//快速排序
// void
// t_quickSort(struct IndexValue* nums,int Begin,int End){
//     int i = Begin;
//     int j = End;
//     struct IndexValue temp = nums[Begin];
//     if(i < j){
//         while(i<j){
//             while(i<j && nums[j].val>temp.val)
//                 j--;
//             if(i<j)
//                 nums[i++] = nums[j];
//             while(i<j && nums[i].val<temp.val)
//                 i++;
//             if(i<j)
//                 nums[j--] = nums[i];
//         }
//         nums[i] = temp;
//         t_quickSort(nums,Begin,j-1);
//         t_quickSort(nums,i+1,End);
//     }
// }

// 二分查找
// int
// binarySearch(struct IndexValue *nums,int target,int Begin,int End){
//     while(End >= Begin){
//         int middle = Begin + ((End - Begin)>>1);
//         if(nums[middle].val > target){
//             End = middle-1;
//         }else if(nums[middle].val < target){
//             Begin = middle+1;
//         }else{
//             return middle;
//         }
//     }
//     return -1;
// }

int* twoSum(int* nums, int numsSize, int target) {
    if(nums == NULL){
        return NULL;
    }
    struct IndexValue *data = create(nums,numsSize);
     //t_quickSort(data,0,numsSize-1);
       quick_sort(data, numsSize);

    int i;
    int *result = (int*)malloc(sizeof(int)*2);
    for(i = 0;i < numsSize;i++){
        int temp = target - data[i].val;
        // int index2 = binarySearch(data,temp,i+1,numsSize);
        struct IndexValue *itemptr = (struct IndexValue*)bsearch(&temp, data, numsSize, sizeof(data[0]), (int(*)(const void*,const void *))cmp);
        if(!itemptr){
            continue;
        }
        int index2 =(int)(itemptr - data);

        if(index2 == -1){
            continue;
        }else{
            result[0] = data[i].index + 1;
            result[1] = data[index2].index + 1;
            if(result[0] > result[1]){
                result[0] = result[0] + result[1];
                result[1] = result[0] - result[1];
                result[0] = result[0] - result[1];
            }
            return result;
        }
    }
    return NULL;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值