FizzBuzz Solution (C++)

  • 0

    C++ Straightforward solution

    class Solution {
        vector<string> fizzBuzz(int n) {
            string A="FizzBuzz";
            string B="Fizz";
            string C="Buzz";
            vector<string> result;
            for(int i=1;i<=n;i++){
            if(i%3==0 && i%5==0) result.push_back(A);
            else if(i%3==0) result.push_back(B);
            else if(i%5==0) result.push_back(C);
            else{ result.push_back(to_string(i));}
            return result;

Log in to reply

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