412. Fizz Buzz

2017-08-12  本文已影响0人  腹黑君

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

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

没啥说的,注意i不能打出来就好,str(就可以了)

上一篇 下一篇

猜你喜欢

热点阅读