Fizz Buzz

作者: yatttto | 来源:发表于2017-03-13 17:02 被阅读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”.

Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

Code:对1-n的整数做除法是否能被3,5或同时被整除。

class Solution(object):
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        return ['Fizz'*(not n%3)+'Buzz'*(not n%5) or str(i) for i in range(1,n+1)]

相关文章

  • 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逐个输出到...

  • Day 2: Prepare For FizzBuzz.z ->

    Fizz Buzz in Tensorflow interviewer: Welcome, can I get y...

  • 【LeetCode】Fizz Buzz 解题报告

    【LeetCode】Fizz Buzz 解题报告 [LeetCode] https://leetcode.com/...

  • 【LeetCode】Fizz Buzz 解题报告

    【LeetCode】Fizz Buzz 解题报告 [LeetCode] https://leetcode.com/...

  • Fizz Buzz

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

  • Fizz Buzz

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

  • Fizz Buzz

    问题描述 https://www.lintcode.com/problem/fizz-buzz/descripti...

  • Fizz Buzz

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

  • Fizz Buzz

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/fizz-b...

网友评论

      本文标题:Fizz Buzz

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