Topic links: https://leetcode-cn.com/problems/fizz-buzz/
Title Description
Writing a program, it outputs a number from 1 to n strings.
-
If n is a multiple of 3, the output "Fizz";
-
If n is a multiple of 5, the output "Buzz";
3. If n is a multiple of 3 and 5 at the same time, the output "FizzBuzz".
Example:
n = 15,
返回:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
Code
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> s;
for(int i = 1;i<=n;++i){
string tmp;
if(!(i%3) && !(i%5))
tmp = "FizzBuzz";
else if(!(i%3))
tmp = "Fizz";
else if(!(i%5))
tmp = "Buzz";
else
tmp = to_string(i);
s.push_back(tmp);
}
return s;
}
};