当前位置:网站首页>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 lsMethod 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边栏推荐
- Investment operation steps
- What if software testing is too busy to study?
- MySQL数据库(三)高级数据查询语句
- 软件测试需求分析之什么是“试纸测试”
- Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- 软件测试面试回答技巧
- The latest query tracks the express logistics and analyzes the method of delivery timeliness
- MySQL数据库(五)视 图 、 存 储 过 程 和 触 发 器
- [oiclass] share prizes
猜你喜欢

Express

Report on the double computer experiment of scoring system based on 485 bus

Threads and thread pools

Description of Vos storage space, bandwidth occupation and PPS requirements

Leetcode notes - dynamic planning -day6
软件测试方法有哪些?带你看点不一样的东西

Maximum nesting depth of parentheses in leetcode simple questions

CSAPP shell lab experiment report

ucore lab 6

MySQL数据库(一)
随机推荐
What is "test paper test" in software testing requirements analysis
接口测试面试题及参考答案,轻松拿捏面试官
线程及线程池
安全测试入门介绍
Servlet
CSAPP家庭作業答案7 8 9章
pytest
What to do when programmers don't modify bugs? I teach you
Investment should be calm
Which version of MySQL does php7 work best with?
ucore lab7 同步互斥 实验报告
Brief introduction to libevent
Express
Emqtt distribution cluster and node bridge construction
How to become a good software tester? A secret that most people don't know
Want to change jobs? Do you know the seven skills you need to master in the interview software test
Portapack application development tutorial (XVII) nRF24L01 launch B
Your wechat nickname may be betraying you
Automated testing problems you must understand, boutique summary
CSAPP homework answers chapter 789