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.
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) 
{
    for(int i = 0;i < numsSize;i++)
    {
        for(int j = i+1;j < numsSize;j++)
        {
            if(nums[i]+nums[j] == target)
            {
                int *arr = (int *)malloc(2*sizeof(int));
                arr[0] = i;
                arr[1] = j;
                return arr;
                
            }
        }
    }
    return NULL;   
}

经验与教训

break只跳出当前循环,在上面的代码中只跳出最内层循环,因此如果不在满足条件时return的话,需要相应的标志flag,在两个for循环中都要去判断flag,初始值flag为1,if条件成立后,置flag为0,然后在循环外面申请内存,返回指针,赋值时要注意由于for循环不满足,i和j多加了一次,需要赋值i-1,j-1,因此上面的代码从目前来看是最优解法。