C语言 | Leetcode C语言题解之第412题Fizz Buzz
题目:
题解:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
char ** fizzBuzz(int n, int* returnSize)
{
/*定义字符串数组*/
char **answer = (char**)malloc(sizeof(char*)*n);
for(int i =1;i<=n;i++)
{
/*分配单个字符串的SIZE*/
answer[i-1] = (char*)malloc(sizeof(char)*9);
/*根据条件处理分支*/
if(i % 3 == 0 && i % 5 == 0)
{
strcpy(answer[i-1],"FizzBuzz");
}
else if( i % 3 == 0)
{
strcpy(answer[i-1],"Fizz");
}
else if(i % 5 == 0)
{
strcpy(answer[i-1],"Buzz");
}
else
{
/*int sprintf(char *string, char *format [,argument,…]);*/
sprintf(answer[i-1],"%d",i);
}
}
*returnSize = n;
return answer;
}