Leetcode 412 Fizz Buzz
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 if non of the above conditions are true.
Input: n = 3
Output: ["1","2","Fizz"]
Input: n = 5
Output: ["1","2","Fizz","4","Buzz"]
- Soruda en çok sorulan sorulardan olan FizzBuzz sorusu.
- Burada dikkat edilmesi gereken ilk olarak hem 3 e hem 5 e bölüneni kontrol etmek.
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
for i in range(1, n+1):
if i % 3 == 0 and i % 5 == 0:
# i is multiple of both 3 and 5
res.append('FizzBuzz')
elif i % 3 == 0:
# i is multiple of 3 only
res.append('Fizz')
elif i % 5 == 0:
# i is mupltiple of 5 only
res.append('Buzz')
else:
# i is neither multiple of 3 nor multiple of 5.
res.append( str(i) )
return res