Skip to content

Instantly share code, notes, and snippets.

@fffaraz
Last active September 5, 2025 02:33
Show Gist options
  • Save fffaraz/063fa519b6027c1ad45470ccc5fe5b8b to your computer and use it in GitHub Desktop.
Save fffaraz/063fa519b6027c1ad45470ccc5fe5b8b to your computer and use it in GitHub Desktop.
FizzBuzz leetcode
// https://leetcode.com/problems/fizz-buzz/
class Solution {
public:
const char* fizzbuzz[4] = { nullptr, "Fizz", "Buzz", "FizzBuzz" };
const uint8_t pattern[15] = { 3, 0, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 0 };
vector<string> fizzBuzz(int n)
{
vector<string> result(n);
for (int i = 1, m = 1; i <= n; ++i, ++m) {
if (m == 15) m = 0;
const uint8_t x = pattern[m];
result[i - 1] = (x == 0) ? std::to_string(i) : fizzbuzz[x];
}
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment