3ms C solution , Easy to understand.


  • 0
    H

    /**

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

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

Log in to reply
 

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