Leetcode 1. Two Sum with C

来源:互联网 发布:5118的子域名查询工具 编辑:程序博客网 时间:2024/06/01 07:22

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.

Example:

Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].

/** * Note: The returned array must be malloced, assume caller calls free(). */int* twoSum(int* nums, int numsSize, int target) {    int reset_num = 0;    int *result = (int*)malloc(sizeof(int)*2);    int start = 0;    int i = 0;    while(start != numsSize)    {        reset_num = target - nums[start++];        for (i = start; i != numsSize; i++)        {            if (nums[i] == reset_num)            {                result[0] = start -1;                result[1] = i;                return result;            }        }            }    return NULL;}





0 0