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"
]
class Solution(object):def fizzBuzz(self, n):""":type n: int:rtype: List[str]"""#return ["FizzBuzz"*(i%15==0) or "Buzz"*(i%5==0) or "Fizz"*(i%3==0) or str(i) for i in range(1, n+1)]m = p = 0       ans = []for i in range(1, n+1):m += 1p += 1s = str(i)if m == 3 and p == 5:s = "FizzBuzz"m = p = 0elif m == 3:s = "Fizz"m = 0elif p == 5:s = "Buzz"p = 0ans.append(s)            return ans        

分别是用求余版本和不用求余版本。

记住 if else完全可以用and or 替代!


转载于:https://www.cnblogs.com/bonelee/p/8526045.html

leetcode 412. Fizz Buzz相关推荐

  1. 【leetcode】412.Fizz Buzz (三种方法开阔思路,java实现)

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

  2. 【快乐水题】412. Fizz Buzz

    原题: 力扣链接:412. Fizz Buzz 题目简述: 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中 ...

  3. 【跟Leon一起刷LeetCode】412. Fizz Buzz

    Fizz Buzz Description: Write a program that outputs the string representation of numbers from 1 to n ...

  4. LeetCode 1195. Fizz Buzz Multithreaded--并发系列题目--Java 解法--AtomicInteger/CountDownLatch/CyclicBarrier

    题目地址:Fizz Buzz Multithreaded - LeetCode Write a program that outputs the string representation of nu ...

  5. 412. Fizz Buzz(java)

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

  6. LeetCode之Fizz Buzz

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

  7. 412. Fizz Buzz

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

  8. Java/412.Fizz Buzz

    题目 代码部分(2ms 98.26%) class Solution {public List<String> fizzBuzz(int n) {List<String> re ...

  9. LeetCode412_412. Fizz Buzz

    LeetCode412_412. Fizz Buzz 一.描述 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果, ...

最新文章

  1. HBase - Phoenix剖析
  2. 假如有人把支付宝所有存储服务器炸了,我们在里边的钱是不是都丢了?
  3. 大牛书单 | 大数据存储方向好书分享
  4. mysql 每条记录大小_计算数据库中各个表的数据量和每行记录所占用空间
  5. 建模元件有哪些在MapleSim中
  6. 演练 玩游戏支付游戏币
  7. java 任务池_多线程的应用-异步任务线程池的简单实现
  8. android httppost
  9. DNSBIND——DNS的ACL和视图
  10. Oracle基础查询
  11. 【开源】EasyDarwin编译全过程:Linux系统下编译运行最新版EasyDarwin的步骤介绍
  12. 微信自动回复和群聊消息完善
  13. D. Berserk And Fireball(模拟)
  14. Tech Talk| Redmi K50 电竞版手机极致散热技术详解
  15. 北大计算机研究生有多神仙,北大考研成绩公布,还上“热搜”,这都是些什么“神仙分数”?...
  16. python做相册_Python编程:制作电子相册
  17. 中国B2B跨境电子商务物流发展研究
  18. struts2标签的使用(一)
  19. Button控件的使用方法
  20. Boost.Spirit.Karma 自定义directive

热门文章

  1. linux模块化机制,Linux模块化机制和module_init
  2. shell date 获取昨天日期
  3. 免费Java高级工程师学习资源,使用指南
  4. 基于Pytorch再次解读GoogLeNet现代卷积神经网络
  5. Android移动开发之【Android实战项目】Textview各项属性(全)
  6. 2019-2020 ACM-ICPC Brazil Subregional Programming Contest
  7. python基础练习(三)
  8. html自动兼容像素密度,解决 HTML Canvas 元素在高像素密度/高分辨率屏幕上显示模糊的问题...
  9. python不能加密_Python之加密模块
  10. php隐藏webshell_【web端权限维持】利用ADS隐藏webshell