412. Fizz Buzz

Write 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”.

 

 

class Solution(object):
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        output = []
        
        for i in range(n):
            i+=1
            if (i % 3 == 0 and i % 5 == 0):
                output.append("FizzBuzz")
            elif (i % 3 == 0):
                output.append("Fizz")
            elif (i % 5 == 0):
                output.append("Buzz")
            else:
                output.append(str(i))
        
        return output

 

以上

posted on 2018-08-24 15:38  jydd  阅读(89)  评论(0编辑  收藏  举报

导航