c语言力扣的题出现的问题

img

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int i,j;
int res[2];
int *result=res;
for(i=0;i<numsSize;i++)
    for(j=i+1;j<numsSize;j++)
        if(nums[i]+nums[j]==target)
        {
            res[0]=i;
            res[1]=j;
        } 
        return result;
}

不知道那有错,求教导?

程序要求的是返回数组是动态分配的,你返回的是局部变量

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