Leetcode Fizz Buzz

 

写一个程序,输出从 1 到 n 数字的字符串表示。

1. 如果 是3的倍数,输出“Fizz”;

2. 如果 是5的倍数,输出“Buzz”;

3.如果 同时是3和5的倍数,输出 “FizzBuzz”。

示例:

n = 15,

返回:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

思路:放松放松,休息一会儿~

char ** fizzBuzz(int n, int* returnSize){

    int i;
    int **ret = malloc(sizeof(char *) * n);
    *returnSize = n;
    
    for(i = 1; i <= n; i++) {
        
        if(i % 15 == 0) {
            ret[i-1] = malloc(sizeof(char)*9);
            strcpy(ret[i-1],"FizzBuzz");
        } else if (i % 3 == 0){
            ret[i-1] = malloc(sizeof(char)*5);
            strcpy(ret[i-1],"Fizz");
        } else if (i % 5 == 0){
            ret[i-1] = malloc(sizeof(char)*5);
            strcpy(ret[i-1],"Buzz");
        } else {
            ret[i-1] = malloc(sizeof(char)*5);
            sprintf(ret[i-1],"%d",i);
        }
        
    }

    return ret;
}

 

;