当前位置:网站首页>LeetCode_ String_ Simple_ 412.Fizz Buzz
LeetCode_ String_ Simple_ 412.Fizz Buzz
2022-07-02 15:15:00 【I'm up and down in the Jianghu】
1. subject
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”]
Tips :
1 <= n <= 104
source : Power button (LeetCode)
link :https://leetcode.cn/problems/fizz-buzz
2. Ideas
(1) simulation
The topic is relatively simple , have access to if-else Statement to judge each situation , And use answer To save the corresponding results .
3. Code implementation (Java)
// Ideas 1———— simulation
class Solution {
public List<String> fizzBuzz(int n) {
List<String> answer = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) {
answer.add("FizzBuzz");
} else if (i % 3 == 0) {
answer.add("Fizz");
} else if (i % 5 == 0) {
answer.add("Buzz");
} else {
answer.add(i + "");
}
}
return answer;
}
}
边栏推荐
- 关于网页中的文本选择以及统计选中文本长度
- [noi Simulation Competition] scraping (dynamic planning)
- Niuke Practice 101
- How to solve the problem of database content output
- 08_ 串
- Learn the method code example of converting timestamp to uppercase date using PHP
- 数据库内容输出有问题怎么解决
- C # delay, start the timer in the thread, and obtain the system time
- 06_栈和队列转换
- 哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
猜你喜欢

Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting

CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E

18_Redis_Redis主从复制&&集群搭建

btrace-(字节码)动态跟踪工具

.NET Core 日志系统

IE 浏览器正式退休

14_Redis_乐观锁

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

Learn the method code example of converting timestamp to uppercase date using PHP
![[c voice] explain the advanced pointer and points for attention (2)](/img/fb/515e25899bd9a2905ee63cb041934a.png)
[c voice] explain the advanced pointer and points for attention (2)
随机推荐
C thread transfer parameters
如何对 TiDB 进行 TPC-C 测试
Record an error report, solve the experience, rely on repetition
16_Redis_Redis持久化
学习使用php实现公历农历转换的方法代码
Mavn 搭建 Nexus 私服
TiDB数据迁移工具概览
LeetCode 2320. 统计放置房子的方式数
The past and present lives of visual page building tools
C#延时、在线程中开启定时器、获取系统时间
Jenkins Pipeline 应用与实践
C语言实现N皇后问题
Deploy tidb cluster with tiup
Kityformula editor configure font size and spacing
20_Redis_哨兵模式
解决el-radio-group 回显后不能编辑问题
15_Redis_Redis.conf详解
02_ Linear table_ Sequence table
AtCoder Beginner Contest 254
02_线性表_顺序表