当前位置:网站首页>LeetCode#412. Fizz Buzz
LeetCode#412. Fizz Buzz
2022-07-06 15:21:00 【Rufeng ZHHH】
subject ( Power button ):
Give you an integer n , Find out from 1 To n Of each integer Fizz Buzz Express , And use string array answer( Subscript from 1 Start ) Return results , among :
answer[i] == "FizzBuzz" If i At the same time 3 and 5 Multiple .
answer[i] == "Fizz" If i yes 3 Multiple .
answer[i] == "Buzz" If i yes 5 Multiple .
answer[i] == i ( In string form ) If none of the above conditions are met .
Example 1:
Input :n = 3
Output :["1","2","Fizz"]
Example 2:
Input :n = 5
Output :["1","2","Fizz","4","Buzz"]
This question is not difficult , It can be written in two ways .
Method 1 :
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
ls=[]
for i in range(1,n+1):
if i%3==0 and i%5==0:
ls.append("FizzBuzz")
elif i%3==0:
ls.append("Fizz")
elif i%5==0:
ls.append("Buzz")
else:
ls.append(str(i))
return ls
Method 2 :
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
answer=[0]*n
for i in range(1,n+1):
if i%3==0 and i%5==0:
answer[i-1]="FizzBuzz"
elif i%3==0:
answer[i-1]="Fizz"
elif i%5==0:
answer[i-1]="Buzz"
else:
answer[i-1]=str(i)
return answer
边栏推荐
- Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
- What to do when programmers don't modify bugs? I teach you
- Scoring system based on 485 bus
- How to write the bug report of software test?
- Eigen User Guide (Introduction)
- Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
- The most detailed postman interface test tutorial in the whole network. An article meets your needs
- CSAPP家庭作业答案7 8 9章
- JDBC introduction
- Réponses aux devoirs du csapp 7 8 9
猜你喜欢
The most detailed postman interface test tutorial in the whole network. An article meets your needs
Want to change jobs? Do you know the seven skills you need to master in the interview software test
Eigen User Guide (Introduction)
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
几款开源自动化测试框架优缺点对比你知道吗?
Investment operation steps
CSAPP homework answers chapter 789
The minimum number of operations to convert strings in leetcode simple problem
Take you to use wxpy to create your own chat robot (plus wechat interface basic data visualization)
Sleep quality today 81 points
随机推荐
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
自动化测试中敏捷测试怎么做?
Servlet
CSAPP Shell Lab 实验报告
UCORE lab8 file system experiment report
如何成为一个好的软件测试员?绝大多数人都不知道的秘密
Sorting odd and even subscripts respectively for leetcode simple problem
基于485总线的评分系统双机实验报告
MySQL数据库(五)视 图 、 存 储 过 程 和 触 发 器
C4D quick start tutorial - Introduction to software interface
Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
Brief introduction to libevent
Stc-b learning board buzzer plays music
Express
ucore lab5用户进程管理 实验报告
Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
ArrayList set
A method and implementation of using VSTO to prohibit excel cell editing
JDBC介绍