leetcode第1题 ,Two Sum

来源:互联网 发布:mac截图存在哪里 编辑:程序博客网 时间:2024/06/06 09:21

题目:Two Sum
LeetCode:https://leetcode.com/problems/two-sum/description/

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

思路:本题是给定一个未排序数组,和一个目标值,求解数组中两个值相加为该目标值的两个数据得到索引。
由于未进行排序,如果采用循环遍历,那么时间复杂度将会达到O(n×n).

int* twoSum1(int* nums,int numsSize,int target){    int i,j;    int* retArr = (int*)malloc(2 * sizeof(int));    if(nums == NULL)    {        return NULL;    }    if(numsSize < 2 )    {        return NULL;    }    int diff;    for(i = 0 ; i < numsSize - 1 ;i++)    {        diff = target - *(nums+i);        for(j = i+1; j < numsSize; j++ )        {            if(*(nums + j)  == diff )            {                *retArr = i;                *(retArr + 1) = j;                return retArr;            }        }    }}

如果给定的数组为排序递增数组,则使用以下方法。
求解的思路是:
比如数组为 [1,3,6,9,12,17];目标值为15。
设定两个索引:
int lowIndex,从;
lowIndex = 0;
hightIndex = numsSize - 1;

求解 arr[lowIndex] + arr[hightIndex]的和,与目标值进行比较。
比如:1+17 = 18 > 15;
则hightIndex–,1+12 = 13 < 15
则lowIndex++,3+12 = 15 == 15 ,因此,所求的数值极为3和12。

#include<stdio.h>#include<stdlib.h>int* twoSum(int* nums,int numsSize,int target);int* testArrNull;int  testArr[10] = {12,54,87,98,124,149,257,457,789,1234};int* targetArr;int main(int argc ,char *argv[]){    printf("\r\n********systerm start **************\r\n");    //test1 :  null nums    targetArr =  twoSum(testArrNull,10,36);    if(targetArr == NULL)    {        printf("test 1:error test\r\n");    }    else     {        printf("test 1 :value1 = %d, value2 = %d\r\n",*targetArr,*(targetArr + 1));    }    //test2 :  length < 2    targetArr =  twoSum(testArr,1,36);    if(targetArr == NULL)    {        printf("test 2: error test\r\n ");    }    else     {        printf("test 2: value1 = %d, value2 = %d\r\n",*targetArr,*(targetArr + 1));    }    //test3 : nums    targetArr =  twoSum(testArr,10,406);    if(targetArr == NULL)    {        printf("test 3:error test\r\n ");    }    else     {        printf("test 3 :value1 = %d, value2 = %d\r\n",*targetArr,*(targetArr + 1));    }    //test4 :  null nums    targetArr =  twoSum(testArr,10,544);    if(targetArr == NULL)    {        printf("test 4:error test\r\n ");    }    else     {        printf("test 4 :value1 = %d, value2 = %d\r\n",*targetArr,*(targetArr + 1));    }    //test5 :  null nums    targetArr =  twoSum(testArr,10,801);    if(targetArr == NULL)    {        printf("test 5:error test\r\n ");    }    else     {        printf("test 5 :value1 = %d, value2 = %d\r\n",*targetArr,*(targetArr + 1));    }    printf("\r\n********systerm finish **************\r\n");    return 1;}int* twoSum(int* nums,int numsSize,int target){    int lowIndex,hightIndex;    int index;    int targetTemp = 0;    int* retArr = (int*)malloc(2 * sizeof(int));    lowIndex = 0;     hightIndex = numsSize - 1;    if(nums == NULL)    {        return NULL;    }    if(numsSize < 2 )    {        return NULL;    }    for(index = 0; index < numsSize ; index ++)    {        targetTemp = nums[lowIndex] + nums[hightIndex];        if(targetTemp > target)        {            hightIndex--;        }        else if(targetTemp < target)        {            lowIndex++;        }        else         {            break;        }        if(hightIndex == lowIndex)        {            return NULL;        }        printf("lowIndex:%d, hightIndex:%d,targetTemp = %d,target = %d \r\n",lowIndex,hightIndex,targetTemp,target);    }    *retArr = lowIndex;    *(retArr + 1) = hightIndex;    return retArr;}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 健身按摩椅 青岛健身 健身好吗 健身超 健身步道 力得健身 健身走 健身工作室 健康健身 快速健身 健身房健身 中国健身 关于健身 健身的方法 健身后 健身球 青橙健身 在家里健身 中老年健身 健身多少钱 健身套装 每天健身 健身舞蹈 健身的方式 健身架 智能健身 健身下载 健身吃 威尔士健身 健身设备 大健身 健身教程 女健身 舒适堡健身 健身教学 健身训练 黑科技健身 健身信息 女性健身 怎么当健身 免费健身