給你一個整數(shù)n. 從 1 到 n 按照下面的規(guī)則打印每個數(shù):
如果這個數(shù)被3整除瞭亮,打印fizz.
如果這個數(shù)被5整除汽绢,打印buzz.
如果這個數(shù)能同時被3和5整除宾符,打印fizz buzz.
image.png
class Solution {
public:
/**
* param n: As description.
* return: A list of strings.
*/
vector<string> fizzBuzz(int n) {
vector<string> results;
for (int i = 1; i <= n; i++) {
if (i % 15 == 0) {
results.push_back("fizz buzz");
} else if (i % 5 == 0) {
results.push_back("buzz");
} else if (i % 3 == 0) {
results.push_back("fizz");
} else {
results.push_back(to_string(i));
}
}
return results;
}
};