美文网首页
412. Fizz Buzz

412. Fizz Buzz

作者: 腹黑君 | 来源:发表于2017-08-12 17:20 被阅读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(就可以了)

相关文章

  • LeetCode 411-430

    412. Fizz Buzz[https://leetcode-cn.com/problems/fizz-buzz...

  • Leetcode PHP题解--D40 412. Fizz Bu

    412. Fizz Buzz 题目链接 412. Fizz Buzz 题目分析 这个题目也很简单。 从1逐个输出到...

  • Leetcode 412 Fizz Buzz

    题目链接:412. Fizz Buzz 题目描述 Write a program that outputs the...

  • 412. Fizz Buzz

    一、题目原型: 写一个程序,输出从 1 到 n 数字的字符串表示。 如果 n 是3的倍数,输出“Fizz”;如果 ...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

    题目分析 题目链接,登录 LeetCode 后可用思路比较简单,直接遍历1 到 n 的每个数,依次判断每种情况即可...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

  • 412. Fizz Buzz

网友评论

      本文标题:412. Fizz Buzz

      本文链接:https://www.haomeiwen.com/subject/jmejrxtx.html