当前位置:网站首页>LeetCode_字符串_简单_412.Fizz Buzz
LeetCode_字符串_简单_412.Fizz Buzz
2022-07-02 11:24:00 【一瓢江湖我沉浮】
1.题目
给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中:
answer[i] == “FizzBuzz”,如果 i 同时是 3 和 5 的倍数。
answer[i] == “Fizz”,如果 i 是 3 的倍数。
answer[i] == “Buzz”,如果 i 是 5 的倍数。
answer[i] == i (以字符串形式),如果上述条件全不满足。
示例 1:
输入:n = 3
输出:[“1”,“2”,“Fizz”]
示例 2:
输入:n = 5
输出:[“1”,“2”,“Fizz”,“4”,“Buzz”]
示例 3:
输入:n = 15
输出:[“1”,“2”,“Fizz”,“4”,“Buzz”,“Fizz”,“7”,“8”,“Fizz”,“Buzz”,“11”,“Fizz”,“13”,“14”,“FizzBuzz”]
提示:
1 <= n <= 104
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/fizz-buzz
2.思路
(1)模拟
本题较为简单,可以使用 if-else 语句来判断每一种情况,并用 answer 来进行保存相应的结果即可。
3.代码实现(Java)
//思路1————模拟
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;
}
}
边栏推荐
- Openharmony notes --------- (4)
- Generally speaking, if the error of inconsistent tab and space occurs frequently
- 3、函数指针和指针函数
- Threejs controller cube space basic controller + inertia control + flight control
- < schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
- obsidian安装第三方插件——无法加载插件
- threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
- Database connection pool and data source
- taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
- Convolutional neural network (Introduction)
猜你喜欢
PyQt5_ Qscrollarea content is saved as a picture
腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
微信小程序使用towxml显示公式
Design and implementation of car query system based on php+mysql
Yyds dry goods inventory software encryption lock function
TeamTalk源码分析之win-client
MathML to latex
How kaggle uses utility script
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
随机推荐
Bit by bit of OpenCV calling USB camera
Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
##51单片机实验之简易验证码发生器
What is erdma? Popular science cartoon illustration
Fabric. Usage of JS eraser (including recovery function)
Daily learning 3
NLA natural language analysis realizes zero threshold of data analysis
Development and design of animation surrounding mall sales website based on php+mysql
Database connection pool and data source
Obsidian installs third-party plug-ins - unable to load plug-ins
Uniapp automated test learning
数据库连接池和数据源
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
Makefile separates file names and suffixes
STM32库函数进行GPIO初始化
Fabric.js 上划线、中划线(删除线)、下划线
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
Fabric. JS upper dash, middle dash (strikethrough), underline
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
A white hole formed by antineutrons produced by particle accelerators