Last active
September 5, 2025 02:33
-
-
Save fffaraz/063fa519b6027c1ad45470ccc5fe5b8b to your computer and use it in GitHub Desktop.
FizzBuzz leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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