题目要求

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”.

题目分析及思路

给定一正整数n,要求输出1-n的所有数字的的字符表示。其中三的倍数的数字输出“Fizz”,五的倍数的数字输出“Buzz”,若同时是三和五的倍数则输出“FizzBuzz”。可以遍历这个范围的所有数字,依次判断每个数字,对应输出它的字符表示。

python代码

class Solution:

def fizzBuzz(self, n: int) -> List[str]:

ans = []

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

if i % 3 == 0 and i % 5 == 0:

ans.append('FizzBuzz')

elif i % 3 == 0:

ans.append('Fizz')

elif i % 5 == 0:

ans.append('Buzz')

else:

ans.append(str(i))

return ans

LeetCode 412 Fizz Buzz 解题报告的更多相关文章

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 方法一:遍历判断 方法二:字符串相加 方法三:字典 日期 [L ...

  2. Java实现 LeetCode 412 Fizz Buzz

    412. Fizz Buzz 写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz" ...

  3. [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

  4. LeetCode 412. Fizz Buzz

    Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...

  5. LeetCode: 412 Fizz Buzz(easy)

    题目: Write a program that outputs the string representation of numbers from 1 to n. But for multiples ...

  6. LeetCode - 412. Fizz Buzz - ( C++ ) - 解题报告 - to_string

    1.题目大意 Write a program that outputs the string representation of numbers from 1 to n. But for multip ...

  7. 【leetcode】412. Fizz Buzz

    problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...

  8. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  9. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

随机推荐

  1. 【Linux高级驱动】触摸屏驱动的移植

    触摸屏驱动的移植 流程 注意:看框架图 1.添加input.c组件 Device Drivers  ---> Input device support  --->  Generic inp ...

  2. SpringBoot打war包并部署到外部tomcat运行(jar工程改造为正war工程)

    如果你的SpringBoot工程是一个jar工程,而想把它改造成war工程,并打成war包放到外部的tomcat下运行,该怎么修改配置呢?这里以Maven工程为例进行介绍. (1)将pom.xml中的 ...

  3. Numpy合并矩阵

    横向合并:hstack 纵向合并:vstack

  4. 灯箱效果插件Magnific Popup详解

    Magnific Popup 是一个非常优秀的弹出对话框或者灯箱效果插件.它基于jQuery(zepto)开发,使用非常简单,特点就是:非常好用. 官网地址: http://dimsemenov.co ...

  5. <我的股票交易知识汇总与个人感悟_v1.0 (By geman)>

    书在这里 一个完整的股票交易包括选股.买股.持股.卖股四个阶段. 右侧交易,顶是跌出来的,底是涨出来的 一定要敢于止损,设好止损位,严格执行,即使踏空也无怨无悔:资金安全第一位 坚持只买处于上升通道的 ...

  6. Linux下apache activemq的安装与配置

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范 的 JMS Provider实现,尽管JMS规范出台已经是很 ...

  7. IOC容器特性注入第七篇:请求上下文作用域

    Ninject的对象作用域: Transient .InTransientScope() 每次调用创建新实例. Singleton .InSingletonScope() 单例,仅创建一个实例. Th ...

  8. DedeCMS中channelartlist自增参数global.itemindex

    在 dede:channelartlist 标签下,使用: {dede:global.itemindex runphp='yes'} {/dede:global.itemindex} dede:cha ...

  9. ubuntu安装cocos2dx

    操作系统:ubuntu16.04 LTSIDE:Code::blocks 16.01Cocos2dx版本:cocos2d-x 3.11.1这篇随笔将会简要地演示如何在ubuntu下安装cocos2dx ...

  10. day_10 py 字典

    #!/usr/bin/env/python#-*-coding:utf-8-*-'''字典: (就是增加个索引名字,然后归类了一下) infor = {键:值,键:值} 列表存储相同的信息随着列表里面 ...