leetcode_412_FizzBuzz

Posted 荆南山砍柴人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode_412_FizzBuzz相关的知识,希望对你有一定的参考价值。

 

Q:

# 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"
# ]

  


 

A :

class Solution(object):

	# my version
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        return map(self.trans, [i for i in range(1, n + 1)])

    def trans(self, x):
		if x %3 == 0 and x % 5 == 0:
			return "FizzBuzzaam"
		elif x % 3 == 0:
			return "Fizz"
		elif x % 5 == 0:
			return "Buzzaam"
		return str(x)

	# popular versions 比较牛逼的版本
	# 这个理解起来还是有点困难, 暂时没看懂
	# def fizzBuzz(self, n):
	#     return[‘FizzBuzz‘[i%-3&-4:i%-5&8^12]or`i`for i in range(1,n+1)]

	# def fizzBuzz(self, n):
	#     return [‘Fizz‘ * (not i % 3) + ‘Buzz‘ * (not i % 5) or str(i) for i in range(1, n+1)]


if __name__ == ‘__main__‘:
	solution = Solution()
	print solution.fizzBuzz(15)

  

以上是关于leetcode_412_FizzBuzz的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode之412. Fizz Buzz

题目地址(412. Fizz Buzz)

Leetcode刷题100天—412. Fizz Buzz(字符串)—day63

Leetcode刷题100天—412. Fizz Buzz(字符串)—day63

LeetCode 412 Fizz Buzz[判断] HERODING的LeetCode之路

LeetCode 412. Fizz Buzz