当前位置:网站首页>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边栏推荐
- JDBC introduction
- STC-B学习板蜂鸣器播放音乐2.0
- MySQL数据库(二)DML数据操作语句和基本的DQL语句
- JDBC介绍
- 自动化测试中敏捷测试怎么做?
- Mysql database (V) views, stored procedures and triggers
- Oracle foundation and system table
- The number of reversing twice in leetcode simple question
- Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
- Take you to use wxpy to create your own chat robot (plus wechat interface basic data visualization)
猜你喜欢

Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
转行软件测试必需要知道的知识

ucore lab7 同步互斥 实验报告

Nest and merge new videos, and preset new video titles

Do you know the advantages and disadvantages of several open source automated testing frameworks?

软件测试Bug报告怎么写?

UCORE lab5 user process management experiment report
What is "test paper test" in software testing requirements analysis
软件测试面试要问的性能测试术语你知道吗?

UCORE lab1 system software startup process experimental report
随机推荐
Preface to the foundations of Hilbert geometry
Eigen User Guide (Introduction)
Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
Programmers, how to avoid invalid meetings?
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
软件测试方法有哪些?带你看点不一样的东西
MySQL数据库(三)高级数据查询语句
UCORE lab7 synchronous mutual exclusion experiment report
CSAPP家庭作業答案7 8 9章
Dlib detects blink times based on video stream
Investment should be calm
Opencv recognition of face in image
Automated testing problems you must understand, boutique summary
C4D quick start tutorial - Introduction to software interface
几款开源自动化测试框架优缺点对比你知道吗?
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
Your wechat nickname may be betraying you
[C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)
Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share