当前位置:网站首页>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;
}
}
边栏推荐
- C# richTextBox控制显示最大行数
- 实用调试技巧
- 学习使用php实现公历农历转换的方法代码
- 03_線性錶_鏈錶
- C code audit practice + pre knowledge
- Tidb data migration tool overview
- TiDB数据迁移工具概览
- The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
- MFC 定时器使用
- IE 浏览器正式退休
猜你喜欢
[email protected] : The platform “win32“ is incompatible with this module."/>info [email protected] : The platform “win32“ is incompatible with this module.

Practical debugging skills

LeetCode - 搜索二维矩阵

03_线性表_链表

LeetCode 2320. Count the number of ways to place the house

Jenkins Pipeline 应用与实践

XML配置文件

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

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

Set set you don't know
随机推荐
AtCoder Beginner Contest 254
TiDB跨数据中心部署拓扑
Leetcode - Search 2D matrix
Tidb data migration scenario overview
MFC console printing, pop-up dialog box
LeetCode 2310. The number of digits is the sum of integers of K
08_ 串
Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
MFC 控制台打印,弹出对话框
Data analysis thinking analysis methods and business knowledge - business indicators
2021-2022學年編譯原理考試重點[華僑大學]
QML pop-up frame, customizable
解决el-radio-group 回显后不能编辑问题
TiDB数据迁移工具概览
kibana 基础操作
【无标题】LeetCode 2321. 拼接数组的最大分数
Introduction to mathjax (web display of mathematical formulas, vector)
02_线性表_顺序表
你不知道的Set集合