leetcode第1题 ,Two Sum

题目: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值