Leetcode_Two Sum

来源:互联网 发布:数据库管理发展三阶段 编辑:程序博客网 时间:2024/05/16 12:22
/** the first version
 * Note: The returned array must be malloced, assume caller calls free(). */int* twoSum(int* nums, int numsSize, int target) {    int i,j;    int *pos;    int flag = 0;    pos = (int *) malloc(2*sizeof(int));    if(pos == NULL)    {        return NULL;    }    pos[0] = 0;    pos[1] = 0;    for (i = 0; (i < numsSize) && (flag == 0); ++i)    {        for(j = i + 1; (j < numsSize) && (flag == 0); ++ j)        {            if(nums[i] + nums[j] == target)            {                pos[0] = i;                pos[1] = j;                flag = 1;            }        }    }    return pos;


the test result of the first version


0 0