算法描述:算法
給你一個整數n. 從 1 到 n 按照下面的規則打印每一個數:數組
fizz
.buzz
.3
和5
整除,打印fizz buzz
.3
整除也不能被 5
整除,打印數字自己。
好比 n = 15
, 返回一個字符串數組:markdown
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz", "11", "fizz",
"13", "14", "fizz buzz"
]
複製代碼
個人代碼:app
class Solution:
"""
@param n: An integer
@return: A list of strings.
"""
def fizzBuzz(self, n):
# write your code here
str_list = []
for i in range(1,n+1):
if i%3 == 0:
if i%5 == 0:
str_list.append('fizz buzz')
else:
str_list.append('fizz')
else:
if i%5 == 0:
str_list.append('buzz')
else:
str_list.append(str(i))
return str_list
複製代碼