412. Fizz Buzz
Math, String, Simulation ·Problem Statement
link: LeetCode.cn LeetCode
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.
Example:
Input: n = 3
Output: ["1","2","Fizz"]
Input: n = 5
Output: ["1","2","Fizz","4","Buzz"]
Input: n = 15
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Solution Approach
Algorithm
Implement
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
for i in range(n):
if (i + 1) % 3 == 0 and (i + 1) % 5 == 0:
res.append("FizzBuzz")
elif (i + 1) % 3 == 0 :
res.append("Fizz")
elif (i + 1) % 5 == 0:
res.append("Buzz")
else:
res.append(str(i + 1))
return res