412. Fizz Buzz  - leetcode

来源:互联网 发布:安米app源码 编辑:程序博客网 时间:2024/05/22 03:47
  • 412. Fizz Buzz  - leetcode

Write a program that outputs the string representation of numbersfrom 1 to n.

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

n = 15,

 

Return:

[

    "1",

    "2",

    "Fizz",

    "4",

    "Buzz",

    "Fizz",

    "7",

    "8",

    "Fizz",

    "Buzz",

    "11",

    "Fizz",

    "13",

    "14",

    "FizzBuzz"

]

这道题一定是太简单了,几分钟就AC了。。

用了python的list, for in

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

               

原创粉丝点击