C: Runtime Error: Last executed input: 3


  • 0
    L

    Hello:
    The following is my code and I got an error message when I submit my code:

    0_1479784755868_擷取.PNG

    What does that mean? Thank you!

    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    
    char** fizzBuzz(int n, int* returnSize) {
            int i,j;
    	char **arr = (char **)malloc(n*sizeof(char *));
    	for(i=0;i<n;i++)
    	{
    		if((i+1)%15==0)
    		{
    			arr[i] = (char *)malloc(1*sizeof(char));
    			arr[i] = "FizzBuzz";
    		}
    		
    		else if((i+1)%5==0)
    		{
    			arr[i] = (char *)malloc(5*sizeof(char));
    			arr[i] = "Buzz";
    		}
    		else if((i+1)%3==0)
    		{
    			arr[i] = (char *)malloc(5*sizeof(char));
    			arr[i] = "Fizz";
    		}
    		else
    		{
    			arr[i] = (char *)malloc(sizeof(char));
    			sprintf(arr[i],"%d",i+1);
    		}
    	}
    	*returnSize = 0;
    	for(i=0;i<n;i++)
    		*returnSize += strlen(arr[i]);
    	return arr;
    }
    

  • 0
    L

    returnSize should be just the number of the string array in this case.
    So
    *returnSize = n;


  • 0
    B

    The size of your malloc seems wrong.


  • 0
    D

    arr[i] = (char *)malloc(9 * sizeof(char));
    arr[i] = "FizzBuzz";
    FizzBuzz's length is 8. therefore malloc size is 9.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.