当前位置:网站首页>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边栏推荐
- Leetcode simple question: check whether the numbers in the sentence are increasing
- Intensive learning notes: Sutton book Chapter III exercise explanation (ex17~ex29)
- [pytorch] simple use of interpolate
- 基于485总线的评分系统
- CSAPP Shell Lab 实验报告
- Lab 8 file system
- Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
- Brief introduction to libevent
- Report on the double computer experiment of scoring system based on 485 bus
- UCORE lab2 physical memory management experiment report
猜你喜欢

Your wechat nickname may be betraying you

Capitalize the title of leetcode simple question

基于485总线的评分系统双机实验报告
![[HCIA continuous update] advanced features of routing](/img/05/a9ed32ec8c19b236355d48f7c2ad80.jpg)
[HCIA continuous update] advanced features of routing

How to become a good software tester? A secret that most people don't know
自动化测试你必须要弄懂的问题,精品总结
Automated testing problems you must understand, boutique summary

Leetcode simple question: check whether the numbers in the sentence are increasing
软件测试面试要问的性能测试术语你知道吗?

Réponses aux devoirs du csapp 7 8 9
随机推荐
Preface to the foundations of Hilbert geometry
全网最详细的postman接口测试教程,一篇文章满足你
Brief description of compiler optimization level
Heap, stack, queue
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
CSAPP homework answers chapter 789
软件测试行业的未来趋势及规划
The number of reversing twice in leetcode simple question
Leetcode notes - dynamic planning -day6
UCORE lab1 system software startup process experimental report
Should wildcard import be avoided- Should wildcard import be avoided?
The maximum number of words in the sentence of leetcode simple question
Sleep quality today 81 points
Currently, mysql5.6 is used. Which version would you like to upgrade to?
51 lines of code, self-made TX to MySQL software!
[200 opencv routines] 98 Statistical sorting filter
Interview answering skills for software testing
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
Iterators and generators
Interface test interview questions and reference answers, easy to grasp the interviewer