6ms,C language ,easy to understand.


  • 0
    void digit_to_string(char *str, int num)                                 //change digit to string and then write to string
    {
    	int i = 0, j = 0;
    	char s[10];
    	while (num != 0)
    	{
    		s[i] = (num % 10) + '0';
    		num = num / 10;
    		i++;
    	}
    	for (j = 0; i > 0; i--, j++)
    		str[j] = s[i - 1];
    	str[j] = '\0';
    }
    char** fizzBuzz(int n, int* returnSize)
    {
    	char **str = (char **)malloc(sizeof(char *)*n);
    	for (int i = 0; i<n; i++)
    		str[i] = (char *)malloc(sizeof(char) * 10);
    	for (int i = 1; i <= n; i++)
    	{
    		if (i % 3 == 0 && i % 5 == 0)
    			str[i - 1] = "FizzBuzz\0";
    		else if (i % 3 == 0)
    			str[i - 1] = "Fizz\0";
    		else if (i % 5 == 0)
    			str[i - 1] = "Buzz\0";
    		else
    			digit_to_string(str[i - 1], i);
    	}
        *returnSize = n;                                           //returnSize is the number of str's row 
    	return(str);
    }
    

Log in to reply
 

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