Given an array of integers that is already sorted in ascending order, 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
题意:一个排好序的数组,升序。给你一个数,从数组中找到和为这个数的俩索引,索引不是从0开始的。。。。。。且只有一组答案
我们提供的服务有:成都做网站、网站设计、微信公众号开发、网站优化、网站认证、越城ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的越城网站制作公司
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) { //复杂度不行啊! // int i,j; // int *a=(int*)malloc(sizeof(int)*2); // for(i=0;itarget){ j--; } if(numbers[i]+numbers[j] PS:俩for循环果然超时。
躺在床上想,会不会是双指针问题。第二天做完提交,是的,典型的双指针问题。啊哈哈,终于学到了。
分享标题:LeetCode167.TwoSumII-InputarrayissortedC语言
浏览地址:http://cdxtjz.cn/article/ipedhj.html