zoukankan      html  css  js  c++  java
  • 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)
    

      

  • 相关阅读:
    shell test -n -z
    java -d64
    shell export
    topngroupcollector
    stat 查看文件修改时间
    随机30道小学计算题02(修改)
    设计四则运算2程序单元测试用例
    学习进度02
    随机30道小学计算题02
    随机30道小学计算题01
  • 原文地址:https://www.cnblogs.com/Wolfanature/p/7473198.html
Copyright © 2011-2022 走看看