当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

Base64 coding can be understood this way

Practical debugging skills
![[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)

解决el-radio-group 回显后不能编辑问题

LeetCode 2310. The number of digits is the sum of integers of K

List集合&UML图

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

20_Redis_哨兵模式

LeetCode - 搜索二维矩阵
![[noi simulation] Elis (greedy, simulation)](/img/a2/f8c8ab3bc8dd779327be3f76990976.png)
[noi simulation] Elis (greedy, simulation)
随机推荐
07_哈希
MFC A对话框调用B对话框函数并传参
Map introduction
Mavn builds nexus private server
C code audit practice + pre knowledge
Jenkins Pipeline 应用与实践
kibana 基础操作
14_Redis_乐观锁
Table responsive layout tips
20_Redis_哨兵模式
[untitled] leetcode 2321 Maximum score of concatenated array
蜻蜓低代码安全工具平台开发之路
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
04_ Stack
List set & UML diagram
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
Btrace- (bytecode) dynamic tracking tool
IE 浏览器正式退休
如何对 TiDB 进行 TPC-C 测试
C# 线程传参