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 值。
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; }
相关文章推荐
- 第14周项目1小玩文件(1)统计文本文件abc.txt中的字符个数
- openstack【juno】入门高级篇:为何你的实例ping不通外网【详述】
- POJ之路8-1328
- python 列表转换成字符串
- 值得学习的C开源项目
- LIst和table的转换
- python 中内存对数据的应用
- JAVASE学习笔记:第二章 JAVA基本数据类型
- 如何关闭MyEclipse自动更新
- 扫雷:我的Java之路--第一个小作品
- 你我贷CTO冯炯:互联网金融的P2P+O2O怎么做?
- jQuery操作cookie
- leetcode 18 -- 4Sum
- OC第九天
- 关联模型主被动关联的区别
- Populating Next Right Pointers in Each Node
- appium windows下安装环境搭建
- 免费的sip账号
- wireshark数据包分析实战 总结
- 如何取消不能修改的word文件的密码