3ms cpp solution


  • 0
    Z
    #include <string>
    #include <vector>
    class Solution {
    public:
        vector<string> fizzBuzz(int n) {
            std::vector<std::string> ret;
            for (int i = 1; i <= n; ++i) {
                if (i % 15 == 0) {
                    ret.push_back(std::string("FizzBuzz"));
                } else if (i % 3 == 0) {
                    ret.push_back(std::string("Fizz"));
                } else if (i % 5 == 0) {
                    ret.push_back(std::string("Buzz"));
                } else {
                    ret.push_back(std::to_string(i));
                }
            }
            return ret;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.