3-line C++ O(n) solution


  • 0

    Since the requirement is to output all n strings anyway, I think a straightforward approach should be enough. (cannot be in less time than O(n) time anyway)

        vector<string> fizzBuzz(int n) {
          vector<string> res;
          for (int i = 1; i <= n; ++i) res.push_back(i%15? i%3? i%5? to_string(i) : "Buzz" : "Fizz" : "FizzBuzz");
          return res;
        }
    

Log in to reply
 

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