[LeetCode] 412. Fizz Buzz


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”.


n = 15,


It is a simple question. Use one for loop and output the corresponding string that follows the rules.

There is one tip to speed up. We can use constructor of vector to create enough spaces at the declaration because we know the final length from the input. Therefore, we can use index to access element rather than use push_back.

vector<string> res;        => need to use push_back()
vector<string> res(n, ""); => can use index to access
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.