Skip to content

Problem

Given an integer n, return a string array answer (1-indexed) where:

  • answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
  • answer[i] == "Fizz" if i is divisible by 3.
  • answer[i] == "Buzz" if i is divisible by 5.
  • answer[i] == i (as a string) if none of the above conditions are true.

Constraints:

  • 1 <= n <= 104

Example 1:

**Input:** n = 3
**Output:** ["1","2","Fizz"]

Solve

class Solution(object):
    def fizzBuzz(self, n):
        ans = []
        for i in range(1,n+1):
            if i % 3 == i % 5 == 0:
                ans += ["FizzBuzz"]
            elif i % 3 == 0:
                ans += ["Fizz"]
            elif i % 5 == 0:
                ans += ["Buzz"]
            else:
                ans += [str(i)]
        return ans

Last update : October 13, 2023
Created : August 16, 2023