412.Fizz Buzz

412.Fizz Buzz

难度:Easy

写一个程序,输出从 1 到 n 数字的字符串表示。

  1. 如果 n 是3的倍数,输出“Fizz”;

  2. 如果 n 是5的倍数,输出“Buzz”;

3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。

示例:

n = 15,

返回:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

简单题,

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        int s=res.size();
        if(s>=n)
            return vector<string>(res.begin(),res.begin()+n);
        
        for(int i=s+1;i<=n;i++)
            if(i%15==0) res.push_back("FizzBuzz");
            else if(i%5 ==0) res.push_back("Buzz");
            else if(i%3 ==0) res.push_back("Fizz");
            else res.push_back(to_string(i));
        return res;
        
    }
private:
    vector<string> res={"1","2","Fizz","4","Buzz"};
};

Last updated