当前位置:网站首页>Force buckle 4_ 412. Fizz Buzz
Force buckle 4_ 412. Fizz Buzz
2022-07-08 02:15:00 【Don't sleep in class】
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"]
Example 3:
Input :n = 15
Output :["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
source : Power button (LeetCode)
Java solution
class Solution {
public List<String> fizzBuzz(int n) {
List<String> lst = new ArrayList<String>();
for (int i = 0; i < n; i++) {
int num = i + 1;
if (num % 3 == 0 && num % 5 == 0) lst.add("FizzBuzz");
else if (num % 3 == 0) lst.add("Fizz");
else if (num % 5 == 0) lst.add("Buzz");
else lst.add(String.valueOf(num));
//String.valueOf(num) take num Convert to string
}
return lst;
}
}
Python solution
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
ans = []
for i in range(1, n + 1):
s = ""
if i % 3 == 0:
s += "Fizz"
if i % 5 == 0:
s += "Buzz"
if s == "":
s = str(i)
ans.append(s)
return ans
边栏推荐
- How to use diffusion models for interpolation—— Principle analysis and code practice
- 科普 | 什么是灵魂绑定代币SBT?有何价值?
- Semantic segmentation | learning record (3) FCN
- 需要思考的地方
- 【错误】加载h5权重出错AttributeError: ‘str‘ object has no attribute ‘decode‘
- MySQL查询为什么没走索引?这篇文章带你全面解析
- 牛熊周期与加密的未来如何演变?看看红杉资本怎么说
- Node JS maintains a long connection
- Little knowledge about TXE and TC flag bits
- JVM memory and garbage collection-3-object instantiation and memory layout
猜你喜欢
LeetCode精选200道--数组篇
Unity 射线与碰撞范围检测【踩坑记录】
2022年5月互联网医疗领域月度观察
XMeter Newsletter 2022-06|企业版 v3.2.3 发布,错误日志与测试报告图表优化
Introduction to grpc for cloud native application development
ClickHouse原理解析与应用实践》读书笔记(8)
Neural network and deep learning-5-perceptron-pytorch
Completion report of communication software development and Application
JVM memory and garbage collection-3-direct memory
#797div3 A---C
随机推荐
关于TXE和TC标志位的小知识
Talk about the cloud deployment of local projects created by SAP IRPA studio
[knowledge map paper] r2d2: knowledge map reasoning based on debate dynamics
Deeppath: a reinforcement learning method of knowledge graph reasoning
Clickhouse principle analysis and application practice "reading notes (8)
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.1 data operation_ Learning thinking and exercise answers
Leetcode featured 200 channels -- array article
Redismission source code analysis
Ml self realization / logistic regression / binary classification
Redission源码解析
Summary of log feature selection (based on Tianchi competition)
Kwai applet guaranteed payment PHP source code packaging
A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing
#797div3 A---C
Cross modal semantic association alignment retrieval - image text matching
Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
cv2-drawline
Neural network and deep learning-5-perceptron-pytorch
Keras' deep learning practice -- gender classification based on inception V3
Is it necessary for project managers to take NPDP? I'll tell you the answer