当前位置:网站首页>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;
}
}
边栏推荐
- Have you learned the wrong usage of foreach
- Introduction to mathjax (web display of mathematical formulas, vector)
- HUSTPC2022
- 数据分析常见的英文缩写(一)
- 如何对 TiDB 进行 TPC-C 测试
- Arithmetic operations and related exercises in C language
- 20_Redis_哨兵模式
- [untitled] leetcode 2321 Maximum score of concatenated array
- QML pop-up frame, customizable
- 牛客练习赛101
猜你喜欢
IE 浏览器正式退休
【C语言】详解指针的初阶和进阶以及注意点(1)
Why can't programmers who can only program become excellent developers?
Learn the method code example of converting timestamp to uppercase date using PHP
Base64 编码原来还可以这么理解
02_线性表_顺序表
XML配置文件
【无标题】LeetCode 2321. 拼接数组的最大分数
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
XML Configuration File
随机推荐
11_Redis_Hyperloglog_命令
原则、语言、编译、解释
蜻蜓低代码安全工具平台开发之路
【C语音】详解指针进阶和注意点(2)
[solution] educational codeforces round 82
【C语言】详解指针的初阶和进阶以及注意点(1)
Mavn builds nexus private server
[noi Simulation Competition] scraping (dynamic planning)
Introduction to C language -- array
06_栈和队列转换
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
List set & UML diagram
How to write sensor data into computer database
Kibana basic operation
C RichTextBox controls the maximum number of lines displayed
Why can't programmers who can only program become excellent developers?
TiDB数据迁移工具概览
Huawei interview question: no palindrome string
PHP method to get the index value of the array item with the largest key value in the array