标签:
题目描述:给你一个整数n. 从 1 到 n 按照下面的规则打印每个数:
如果这个数被3整除,打印fizz.
如果这个数被5整除,打印buzz.
如果这个数能同时被3和5整除,打印fizz buzz.
样例:比如 n = 15, 返回一个字符串数组:
[]
lintcode中最简单的题目,不讲了,把代码放这:
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 range(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
标签:
原文地址:http://blog.csdn.net/guoziqing506/article/details/51601284