Simple C Solution


  • 0
    T
    #define LEN     15
    
    /**
     * 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;
        char **ret;
        int len;
        ret = (char **)calloc(1, n * sizeof(char **));
        for (i = 0; i < n; i++)
            ret[i] = (char *)calloc(1, 16 * sizeof(char *));
        for (i = 0; i < n; i++) {
            if ((i + 1) % 15 == 0) {
                ret[i] = strdup("FizzBuzz");
            } else if ((i + 1) % 3 == 0) {
                ret[i] = strdup("Fizz");
            } else if ((i + 1) % 5 == 0) {
                ret[i] = strdup("Buzz");
            } else {
                snprintf(ret[i], LEN, "%d", i + 1);
            }
        }
    
        *returnSize = n;
        return ret;
    }
    

Log in to reply
 

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