小白笔记------------------leetcode(412. Fizz Buzz )
2016-11-14 10:30
483 查看
Write a program that outputs the string representation of numbers from 1 to
n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
Example:
Subscribe to see which companies asked this question
注意二维字符串数组的malloc,先确定行数,然后为每行创造空间;注意二维字符串数组每行的赋值用*(p+i)的方式
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
char** fizzBuzz(int n, int* returnSize) {
char **result;
int i = 0, m =16;
result =(char **)malloc( n*sizeof(char *) );
for(i = 0;i < n;i++ )
{
result[i]=(char *)malloc( m * sizeof(char) );
}
for(i=0;i<n;i++)
{
if((i+1)%15==0)
*(result+i)="FizzBuzz";
else if((i+1)%3==0)
*(result+i)="Fizz";
else if((i+1)%5==0)
*(result+i)="Buzz";
else
sprintf(*(result+i), "%d", i+1);
}
*returnSize =n;
return result;
}
n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
Example:
n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ]
Subscribe to see which companies asked this question
注意二维字符串数组的malloc,先确定行数,然后为每行创造空间;注意二维字符串数组每行的赋值用*(p+i)的方式
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
char** fizzBuzz(int n, int* returnSize) {
char **result;
int i = 0, m =16;
result =(char **)malloc( n*sizeof(char *) );
for(i = 0;i < n;i++ )
{
result[i]=(char *)malloc( m * sizeof(char) );
}
for(i=0;i<n;i++)
{
if((i+1)%15==0)
*(result+i)="FizzBuzz";
else if((i+1)%3==0)
*(result+i)="Fizz";
else if((i+1)%5==0)
*(result+i)="Buzz";
else
sprintf(*(result+i), "%d", i+1);
}
*returnSize =n;
return result;
}
相关文章推荐
- LeetCode笔记:412. Fizz Buzz
- LeetCode 412. Fizz Buzz 题解(C++)
- 【leetcode题解】412. Fizz Buzz【E】
- LeetCode —— 412. Fizz Buzz
- [leetcode] 412. Fizz Buzz 解题报告
- 小白笔记---------------------------leetcode(203. Remove Linked List Elements )
- 小白笔记------------leetcode(204. Count Primeszhe)
- 小白笔记------------------------leetcode(7. Reverse Integer)
- 小白笔记--------------------leetcode(283. Move Zeroes)
- 小白笔记----------------leetcode(404. Sum of Left Leaves )
- 小白笔记-----------------------leetcode(28. Implement strStr())
- 【LeetCode-Python】412. Fizz Buzz
- LeetCode-412. Fizz Buzz-Java
- 小白笔记-------------------leetcode(263. Ugly Number )
- 小白笔记---------------------leetcode( 453. Minimum Moves to Equal Array Elements )
- LeetCode 412. Fizz Buzz
- LeetCode之412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 小白笔记-------------------------------leetcode(231. Power of Two)
- 小白笔记-----------------------leetcode(8. String to Integer (atoi) )