两个和 leetcode clang

Two sum leetcode clang

我在练习c语言,这里是练习:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

示例:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].

这是我的尝试:

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

但我收到错误答案:

enter image description here

函数定义不满足评论中指定的要求

  • Note: The returned array must be malloced, assume caller calls free()

另外参数

 int* returnSize

未在您的函数定义中使用。

看起来函数应该按照下面的演示程序所示的方式定义。我假设源数组中的任何元素只能在结果数组中出现一次。

#include <stdio.h>
#include <stdlib.h>

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int *twoSum( int *nums, int numsSize, int target, int *returnSize )
{
    int *result = NULL;
    *returnSize = 0;

    for (int i = 0; i < numsSize; i++)
    {
        for (int j = i + 1; j < numsSize; j++)
        {
            if (nums[i] + nums[j] == target)
            {
                int unique = result == NULL;

                if (!unique)
                {
                    unique = 1;
                    for (int k = 1; unique && k < *returnSize; k += 2)
                    {
                        unique = nums[k] != nums[j];
                    }
                }

                if (unique)
                {
                    int *tmp = realloc( result, ( *returnSize + 2 ) * sizeof( int ) );
                    if (tmp != NULL)
                    {
                        result = tmp;
                        result[*returnSize] = i;
                        result[*returnSize + 1] = j;
                        *returnSize += 2;
                    }
                }
            }
        }
    }

    return result;
}

int main( void )
{
    int a[] = { 2, 7, 11, 15 };
    int target = 9;
    int resultSize;

    int *result = twoSum( a, sizeof( a ) / sizeof( *a ), target, &resultSize );

    if (result)
    {
        for (int i = 0; i < resultSize; i += 2 )
        {
            printf( "%d, %d ", result[i], result[i + 1] );
        }
        putchar( '\n' );
    }

    free( result );
}

程序输出为

0, 1

不过对于我来说,我会像这样声明函数

int *twoSum( const int *nums, size_t numsSize, int target, size_t *returnSize );

蛮力法对这个问题很简单。

int* twoSum(int* arr, int n, int t, int* returnSize){
    int *res=malloc(2*sizeof(int));
    *returnSize=2;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if((arr[i]+arr[j])==t)
            {
                res[0]=i;
                res[1]=j;
                goto exit;
              
            }
        }
        
    }
    exit:
    return res;
}