【LeetCode】412. Fizz Buzz 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
https://leetcode.com/problems/fizz-buzz/
- Total Accepted: 31093
- Total Submissions: 53272
- Difficulty: Easy
##Question
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"
]
题目大意
从1~n这么多数字中中,如果某个位置是3的倍数,把这个数字换成Fizz,如果是5的倍数,把这个数字换成Buzz,如果既是3的倍数又是5的倍数,换成FizzBuzz.
解题方法
方法一:遍历判断
思路很简单,判断是否能特定位置的数字是否能被3和5整除即可。
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
ListReturn = [];
x = 1
while x <= n:
if x % 3 == 0 and x % 5 == 0:
ListReturn.append("FizzBuzz")
elif x % 3 == 0:
ListReturn.append("Fizz")
elif x % 5 == 0:
ListReturn.append("Buzz")
else:
ListReturn.append(str(x))
x += 1
return ListReturn
AC:69 ms
感觉好繁琐,python应该可以很简单。所以参考了别人的跟进如下。
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
return ["Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0)
+ str(i) * (i % 3 != 0 and i % 5 != 0)
for i in range(1, n + 1)]
AC:96 ms
嗯。这个看起来舒服多了。
方法二:字符串相加
如果是5的倍数,就把结果字符串后面加上Buzz即可。这里不能使用elif的判断,因为是15既是3的倍数又是5的倍数,所以需要加上两个字符串。
class Solution:
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
for i in range(1, n + 1):
pos = ""
if i % 3 == 0:
pos += "Fizz"
if i % 5 == 0:
pos += "Buzz"
if not pos:
pos = str(i)
res.append(pos)
return res
方法三:字典
把方法二的判断进行了优化,使用字典保存3和5的字符串的结果对应。
class Solution:
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
strmap = {3 : "Fizz", 5 : "Buzz"}
for i in range(1, n + 1):
pos = ""
for j in [3, 5]:
if i % j == 0:
pos += strmap[j]
if not pos:
pos = str(i)
res.append(pos)
return res
日期
2017 年 1 月 2 日
2018 年 11 月 8 日 —— 项目进展缓慢
【LeetCode】412. Fizz Buzz 解题报告(Python)的更多相关文章
- LeetCode 412 Fizz Buzz 解题报告
题目要求 Write a program that outputs the string representation of numbers from 1 to n. But for multiple ...
- Java实现 LeetCode 412 Fizz Buzz
412. Fizz Buzz 写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz" ...
- [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- LeetCode: 412 Fizz Buzz(easy)
题目: Write a program that outputs the string representation of numbers from 1 to n. But for multiples ...
- 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 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【leetcode】412. Fizz Buzz
problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
随机推荐
- springboot与数据访问之jdbc
官网的starthttps://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter 添加依 ...
- 【GS文献】基因组选择在植物分子育种应用的最新综述(2020)
目录 1. 简介 2. BLUP类模型 3. Bayesian类模型 4. 机器学习 5. GWAS辅助的GS 6. 杂交育种 7. 多性状 8. 长期选择 9. 预测准确性评估 10. GS到植物育 ...
- No.2 R语言在生物信息中的应用—模式匹配
目的: 1. 计算自定义模序在所有蛋白质的匹配位点和次数 2. 输出超过阈值的蛋白质序列到Hit_sequences.fasta 3. Hit_sequences.fasta中序列用小写字母,匹配用大 ...
- 在Linux下搭建nRF51822的开发烧写环境(makefile版)
http://www.qingpingshan.com/m/view.php?aid=394836
- 13.Merge k Sorted Lists
思路:利用map<int,vector<ListNode*> > 做值和指针的映射,最后将指针按值依次链接起来, 时间复杂度O(N),空间O(N) Merge k sorted ...
- 关于SQL中Union和Join的用法
转自帘卷西风的专栏(http://blog.csdn.net/ljxfblog) https://blog.csdn.net/ljxfblog/article/details/52066006 Uni ...
- “equals”有值 与 “==”存在 “equals”只是比较值是否相同,值传递,==地址传递,null==a,避免引发空指针异常,STRING是一个对象==null,对象不存在,str.equals("")对象存在但是包含字符‘''
原文链接:http://www.cnblogs.com/lezhou2014/p/3955536.html "equals" 与 "==" "equa ...
- dart系列之:还在为编码解码而烦恼吗?用dart试试
目录 简介 为JSON编码和解码 UTF-8编码和解码 总结 简介 在我们日常使用的数据格式中json应该是最为通用的一个.很多时候,我们需要把一个对象转换成为JSON的格式,也可以说需要把对象编码为 ...
- 12-gauge/bore shotgun
12-gauge/bore shotgun不是弹夹(magazine)容量为12发的霰(xian)弹枪.[LDOCE]gauge - a measurement of the width or thi ...
- A Child's History of England.15
And indeed it did. For, the great army landing from the great fleet, near Exeter, went forward, layi ...