【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算法积 ...
随机推荐
- Matlab矢量图图例函数quiverkey
Matlab自带函数中不包含构造 quiver 函数注释过程,本文参照 matplotlib 中 quiverkey 函数,构造类似函数为 Matlab 中 quiver 矢量场进行标注. quive ...
- logname
logname命令用来显示用户名称. 语法 logname(选项) 选项 --help:在线帮助: --vesion:显示版本信息.
- 压力测试工具——apchebench(简称ab)
ab的原理 ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx.li ...
- KEGG通路图应该怎么看(转载)
转载:http://www.omicshare.com/forum/thread-107-1-3219.html (出处: OmicShare Forum) 不管是RNA-seq的分析数据,还是蛋白组 ...
- exit(0) exit(1) return() 3个的区别
exit(0):正常运行程序并退出程序: exit(1):非正常运行导致退出程序: return():返回函数,若在主函数中,则会退出函数并返回一值. 详细说: 1. return返回函数值,是关键字 ...
- django数据库增删改查
django中数据库基本操作: 1.同步数据库 python manage.py makemigrations #生成migrations python manage.py migrate #应用mi ...
- springboot热部署与监控
一.热部署 添加依赖+Ctrl+F9 <dependency> <groupId>org.springframework.boot</groupId> <ar ...
- 实现android自动化测试部署与运行Shell脚本分享
我的配置是linux 64, android4.2.2的sdk. 实现的细节都在代码注释里了,变量名以及echo的内容也是说明的一部分. 主流程为: 1.检测是否指定端口的模拟器已经运行,若有则关闭2 ...
- Spring Boot中使用Servlet与Filter
在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...
- ssh 无法使用
ssh 无法运行造成无法远程连接 linux 原因: 我将 /var 目录权限修改成了 777,但 linux 系统出于安全起见,该目录的 7 权限只对 root 用户开放,所以linux 系统认为 ...