LintCode Q9 Fizz Buzz in Python

  • Jinhai ZHOU
  • 2 Minutes
  • 2017年1月24日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
"""
@param n: An integer as description
@return: A list of strings.
For example, if n = 7, your code should return
["1", "2", "fizz", "4", "buzz", "fizz", "7"]
"""
def fizzBuzz(self, n):
results = []
for i in xrange(1, n+1):
if i % 15 == 0:
results.append("fizz buzz")
elif i % 5 == 0:
results.append("buzz")
elif i % 3 == 0:
results.append("fizz")
else:
results.append(str(i))
return results
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。