【LeetCode】412. Fizz Buzz 解题报告(Python)

901 篇文章 208 订阅

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

https://leetcode.com/problems/fizz-buzz/

  • Total Accepted: 31093
  • Total Submissions: 53272
  • Difficulty: Easy

##Question

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

题目大意

从1~n这么多数字中中,如果某个位置是3的倍数,把这个数字换成Fizz,如果是5的倍数,把这个数字换成Buzz,如果既是3的倍数又是5的倍数,换成FizzBuzz.

解题方法

方法一:遍历判断

思路很简单,判断是否能特定位置的数字是否能被3和5整除即可。

class Solution(object):
    def fizzBuzz(self, n):
    	"""
    	:type n: int
    	:rtype: List[str]
    	"""
    	ListReturn = [];
    	x = 1
    	while x <= n:
    		if x % 3 == 0 and x % 5 == 0:
    			ListReturn.append("FizzBuzz")
    		elif x % 3 == 0:
    			ListReturn.append("Fizz")
    		elif x % 5 == 0:
    			ListReturn.append("Buzz")
    		else:
    			ListReturn.append(str(x))
    		x += 1
    	return ListReturn

AC:69 ms

感觉好繁琐,python应该可以很简单。所以参考了别人的跟进如下。

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

AC:96 ms

嗯。这个看起来舒服多了。

方法二:字符串相加

如果是5的倍数,就把结果字符串后面加上Buzz即可。这里不能使用elif的判断,因为是15既是3的倍数又是5的倍数,所以需要加上两个字符串。

class Solution:
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        res = []
        for i in range(1, n + 1):
            pos = ""
            if i % 3 == 0:
                pos += "Fizz"
            if i % 5 == 0:
                pos += "Buzz"
            if not pos:
                pos = str(i)
            res.append(pos)
        return res

方法三:字典

把方法二的判断进行了优化,使用字典保存3和5的字符串的结果对应。

class Solution:
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        res = []
        strmap = {3 : "Fizz", 5 : "Buzz"}
        for i in range(1, n + 1):
            pos = ""
            for j in [3, 5]:
                if i % j == 0:
                    pos += strmap[j]
            if not pos:
                pos = str(i)
            res.append(pos)
        return res

日期

2017 年 1 月 2 日
2018 年 11 月 8 日 —— 项目进展缓慢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值