Back to Problem
Official
2040 views
Editorial: FizzBuzz
by admin ·
19
1 Intuition
Build the word by concatenating the Fizz and Buzz parts.
2 Approach
For each i, append 'Fizz' if divisible by 3 and 'Buzz' if divisible by 5; if nothing was appended, use the number.
3 Complexity Analysis
Time O(n), Space O(n).
4 Solution Code
class Solution:
def fizzBuzz(self, n: int) -> list[str]:
out = []
for i in range(1, n + 1):
s = ('Fizz' if i % 3 == 0 else '') + ('Buzz' if i % 5 == 0 else '')
out.append(s or str(i))
return out
5 Code Walkthrough
The reference solution below implements the approach step by step.