FizzBuzz Solution (C++)


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

Log in to reply
 

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