当前位置:网站首页>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;
}
}
边栏推荐
- [development environment] StarUML tool (download software | StarUML installation | StarUML creation project)
- 一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
- threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
- Fatal: unsafe repository is owned by someone else
- Pychart connects to the remote server
- 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
- How kaggle uses utility script
- [QNX hypervisor 2.2 user manual]6.3 communication between guest and external
- Fabric.js 自由绘制圆形
- 《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
猜你喜欢

The most complete analysis of Flink frame window function

Implement a server with multi process concurrency

Stm32-dac Experiment & high frequency DAC output test

没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法

Tmall product details interface (APP, H5 end)

Fatal: unsafe repository is owned by someone else
![[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment](/img/e1/c8e81570ab78de1e488a611c25ebb9.png)
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment

Xilinx Vivado set *. svh as SystemVerilog Header

Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)

Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
随机推荐
Yolov3 & yolov5 output result description
Design and implementation of car query system based on php+mysql
mathjax 入门(web显示数学公式,矢量的)
Yyds dry goods inventory software encryption lock function
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
Fabric.js 上划线、中划线(删除线)、下划线
Delete element (with transition animation)
Makefile separates file names and suffixes
Fabric. JS dynamically set font size
The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
buuctf-pwn write-ups (7)
uniapp自动化测试学习
STM32 library function for GPIO initialization
Fabric. JS free draw circle
OpenCV调用USB摄像头的点滴
freemarker的使用
taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
mathML转latex
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
HMS core machine learning service helps zaful users to shop conveniently