Problem¶
Given an integer n
, return a string array answer
(1-indexed) where:
answer[i] == "FizzBuzz"
ifi
is divisible by3
and5
.answer[i] == "Fizz"
ifi
is divisible by3
.answer[i] == "Buzz"
ifi
is divisible by5
.answer[i] == i
(as a string) if none of the above conditions are true.
Constraints:
1 <= n <= 104
Example 1:
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
Created : August 16, 2023