Two sum leetcode clang

Advertisements

I practice in c language, here is the exercise:

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.

Example :

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

Here my attempt:

/**
 * 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;
}

But Irecieve a wrong answer:

enter image description here

>Solution :

The function definition does not satisfies the requirement specified in the comment

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

Moreover the parameter

 int* returnSize

is not used within your function definition.

It seems the function should be defined the following way as it is shown in the demonstration program below. I assume that any number in the array can be present in the result array only one time.

#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 );
}

The program output is

0, 1

Though as for me then I would declare the function like

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

Leave a ReplyCancel reply