当前位置:网站首页>Leetcode fizz buzz simple
Leetcode fizz buzz simple
2022-06-13 05:49:00 【AnWenRen】
title :412Fizz Buzz- Simple
subject
Give you an integer
n, Find out from1TonOf each integer Fizz Buzz Express , And use string arrayanswer( 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”]
Tips
1 <= n <= 104
Code Java
public List<String> fizzBuzz(int n) {
ArrayList<String> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0)
list.add("FizzBuzz");
else if (i % 3 == 0)
list.add("Fizz");
else if (i % 5 == 0)
list.add("Buzz");
else {
StringBuilder sb = new StringBuilder(); // StringBuilder Can store any type of string
sb.append(i);
list.add(sb.toString());
}
}
return list;
}
边栏推荐
- A simple recursion problem of linked list
- Input a number and output each digit from high to low
- Mysql database backup and restore:
- How to Algorithm Evaluation Methods
- Feel the power of shardingsphere JDBC through the demo
- Sentinel series hot spot current limiting
- Calculate the number of days between two times (supports cross month and cross year)
- Pychart professional edition's solution to SQL script error reporting
- One of PowerShell optimizations: prompt beautification
- MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
猜你喜欢

MongoDB 多字段聚合Group by

Service architecture diagram of Nacos series

10 signalstartevent and signalcatchingevent of flowable signal events

Wampserver (MySQL) installation

One of PowerShell optimizations: prompt beautification

Why do so many people hate a-spice

About Evaluation Metrics

Automatic database backup (using Navicat)

About Evaluation Metrics

Mysql database crud operation
随机推荐
Use of mongodb
= = relation between int and integer
Quartz basic use
MongoDB 多字段聚合Group by
Parallelgateway and exclusivegateway of 14 gateways
Working principle of sentinel series (source code analysis)
Windbos common CMD (DOS) command set
Set the correct width and height of the custom dialog
2021.9.29学习日志-Restful架构
Conf/tongweb Functions of properties
Misunderstanding of tongweb due to ease of use
Etcd fast cluster building
Feel the power of shardingsphere JDBC through the demo
Agile conflicts and benefits
Browser screenshot method (long screenshot, node screenshot, designated area screenshot)
2021.9.30 learning log -postman
15 inclusivegateway and eventgateway of flowable gateway
The reason why the process cannot be shut down after a spark job is executed and the solution
How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
Sentinel series integrates Nacos and realizes dynamic flow control