Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)

Fizz BuzzWrite a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:
n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]

中文解释:
输入一个数字,返回从1到这个数字的所有数字的字符串,如果遇到3的倍数,就替换成Fizz, 遇到5的倍数,就tihu替换成Buzz,同时是3和5的倍数,就tihu替换成FizzBuzz。
 
我的代码:
    def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""

result=[]
for i in range(1,n+1):
remainder1=i%3
remainder2=i%5
if remainder1 != 0 and remainder2!=0:
result.append(str(i))
elif remainder1==0 and remainder2!=0:
result.append('Fizz')
elif remainder1!=0 and remainder2 == 0:
result.append("Buzz")
else:
result.append("FizzBuzz")

return result

0 个评论

要回复文章请先登录注册