LeetCode 412. Fizz Buzz

Table of Contents

一、中文版

二、英文版

三、My answer

四、解题报告


一、中文版

写一个程序,输出从 1 到 n 数字的字符串表示。

1. 如果 n 是3的倍数,输出“Fizz”;

2. 如果 n 是5的倍数,输出“Buzz”;

3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。

示例:

n = 15,

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fizz-buzz
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二、英文版

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

三、My answer

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        res =[]
        for i in range(1,n+1):
            if i % 3 == 0 and i % 5 == 0:
                res.append("FizzBuzz")
            elif i % 3 == 0:
                res.append("Fizz")
            elif i % 5 == 0:
                res.append("Buzz")
            else:
                res.append(str(i))
        return res
        

四、解题报告

数据结构:数组

实现:if 规则

算法:模拟题目即可

注意:因为本题较为简单,所以可以直接 if 模拟规则,如果题目更为复杂,不可能每种情况都写出 if,即使写出也会很冗杂。可以参见 LeetCode 官方答案第二种方法,摘抄如下:

方法二: 字符串连接
思路

这个方法不会降低渐进复杂度,但是当 FizzBuzz 的规则变得更复杂的时候,这将会是个更优雅的解法。比方说,玩个 FizzBuzzJazz 的游戏。规则如下:
<pre>
3 ---> "Fizz" , 5 ---> "Buzz", 7 ---> "Jazz"
</pre>

如果你还是用之前的方法来解决这个问题的话,那将会有非常多的条件需要判断哦~

能不能被 3 整除
能不能被 5 整除
能不能被 7 整除
能不能同时被 3 和 5 整除
能不能同时被 5 和 7 整除
能不能同时被 3 和 7 整除
能不能同时被 3,5,7 整除
不能被 3,5,7 其中任何一个数整除
如果 FizzBuzz 照着这种方式变地更复杂的话,那么你要写的判断可能会让你抓狂。

算法

我们放弃使用之前的联合判断,取而代之依次判断是否能被给定的数整数。这道题中,就是依次判断能不能被 3 整除,能不能被 5 整除。如果能被 3 整除,就把对应的 Fizz 连接到答案字符串,如果能被 5 整除,就把 Buzz 连接到答案字符串。

举个例子,现在需要判断 15,步骤将会是下面这样的:

条件 1: 15 % 3 == 0, num_ans_str = "Fizz"
条件 2: 15 % 5 == 0, num_ans_str += "Buzz"
=> num_ans_str = "FizzBuzz"

对于 FizzBuzz 来说,只需要判断两个条件就可以了,而不需要像方法一中那样判断三个条件。

同样的,对于 FizzBuzzJazz,现在只需要判断三个条件就可以了。

class Solution:
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        # ans list
        ans = []

        for num in range(1,n+1):

            divisible_by_3 = (num % 3 == 0)
            divisible_by_5 = (num % 5 == 0)

            num_ans_str = ""

            if divisible_by_3:
                # Divides by 3
                num_ans_str += "Fizz"
            if divisible_by_5:
                # Divides by 5
                num_ans_str += "Buzz"
            if not num_ans_str:
                # Not divisible by 3 or 5
                num_ans_str = str(num)

            # Append the current answer str to the ans list
            ans.append(num_ans_str)  

        return ans



作者:LeetCode
链接:https://leetcode-cn.com/problems/fizz-buzz/solution/fizz-buzz-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值