leetcode 412. 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" ]
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 += 1 p += 1 s = str(i) if m == 3 and p == 5: s = "FizzBuzz" m = p = 0 elif m == 3: s = "Fizz" m = 0 elif p == 5: s = "Buzz" p = 0 ans.append(s) return ans
分别是用求余版本和不用求余版本。
记住 if else完全可以用and or 替代!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2017-03-07 公积金联名卡——提取公积金用,用身份证即可办理
2017-03-07 Facebook图片存储系统Haystack——存小文件,本质上是将多个小文件合并为一个大文件来降低io次数,meta data里存偏移量
2017-03-07 HDFS namenode 高可用(HA)搭建指南 QJM方式 ——本质是多个namenode选举master,用paxos实现一致性
2017-03-07 cassandra 存储list数组
2017-03-07 cassandra 存储二进制data
2017-03-07 ubuntu下安装cpython 0.2x
2017-03-07 mongodb存储二进制数据的二种方式——binary bson或gridfs