您的位置:首页 > 其它

C实现 LeetCode-> TwoSum

2015-06-04 15:13 323 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: