码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode_412_FizzBuzz

时间:2017-09-04 14:54:32      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:multi   res   int   out   tip   example   python   output   self   

 

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

标签:multi   res   int   out   tip   example   python   output   self   

原文地址:http://www.cnblogs.com/Wolfanature/p/7473198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!