题目要求

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高级驱动】linux设备驱动模型之平台设备驱动机制

    [1:引言: linux字符设备驱动的基本编程流程] 1.实现模块加载函数  a.申请主设备号    register_chrdev(major,name,file_operations);  b.创 ...

  2. VS2013 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService

    全是2012版本的  没找到2013的! 控制面板>程序>程序和功能 找到如下选中软件右击修复 即可 需关闭VS2013 参考:https://blog.csdn.net/zhaoyun9 ...

  3. 【iCore4 双核心板_ARM】例程二十三:LWIP_HTTP实验——网页服务器

    实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...

  4. STM32 多通道ADC采样,采用Timer1进行采样率控制,利用DMA进行传输

    http://blog.csdn.net/varding/article/details/17559399 http://www.51hei.com/stm32/3842.html https://w ...

  5. Java如何根据主机名(域名)获取IP地址?

    在Java编程中,如何根据主机名(域名)获取IP地址? 以下示例显示了如何通过net.InetAddress类的InetAddress.getByName()方法将主机名更改为指定的IP地址. pac ...

  6. Windows平台使用RMAN命令自动删除Oracle过期归档日志的方法

    自动删除Oracle过期归档日志的思路如下: 1.编写自动执行的bat脚本文件: 2.Windows设置定期执行计划 OracleArchLogClear.bat D:/Oracle/product/ ...

  7. Ubuntu16.04搭建Postfix作为SMTP服务器

    一.DNS配置 类型 名称  值  TTL   A mail  128.199.254.32  1小时  MX  @ mail.example.com(优先:10) 1小时  TXT @  v=spf ...

  8. django 单元测试错误总结

    TestCase django自带有一个TestCase模块来进行测试,我们可以参考官网 来写单元测试的代码.我这里主要是总结一些错误. 用户无法登陆 我们有些api登录后才可以进行测试,所以我们可以 ...

  9. [TF] Architecture - Computational Graphs

    阅读笔记: 仅希望对底层有一定必要的感性认识,包括一些基本核心概念. Here只关注Graph相关,因为对编程有益. TF – Kernels模块部分参见:https://mp.weixin.qq.c ...

  10. [Algorithm] Warm-up puzzles

    闲下来后,需要讲最近涉及到的算法全部整理一下,有个indice,方便记忆宫殿的查找 MIT的算法课,地球上最好:https://ocw.mit.edu/courses/electrical-engin ...