当前位置:网站首页>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;
}
}
边栏推荐
- 4、数组指针和指针数组
- Bit by bit of OpenCV calling USB camera
- 3、函数指针和指针函数
- Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control
- taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
- 《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
- 4. Array pointer and pointer array
- 天猫商品详情接口(APP,H5端)
- What is erdma? Popular science cartoon illustration
- Uniapp automated test learning
猜你喜欢

Yyds dry goods inventory software encryption lock function

Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022

Ad20 cannot select the solution of component packaging in PCB editor

buuctf-pwn write-ups (7)

Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'

Teamtalk source code analysis win client

Factal: Unsafe repository is owned by someone else Solution

Simple verification code generator for 51 single chip microcomputer experiment

JMeter script parameterization

Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
随机推荐
buuctf-pwn write-ups (7)
[QNX hypervisor 2.2 user manual]6.3 communication between guest and external
Yolov6 training: various problems encountered in training your dataset
Fabric.js 橡皮擦的用法(包含恢复功能)
fatal: unsafe repository is owned by someone else 的解决方法
< 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
3、函数指针和指针函数
Fabric. JS free drawing ellipse
Xilinx Vivado set *. svh as SystemVerilog Header
mathML转latex
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
Find the maximum inscribed circle of the contour
STM32 standard firmware library function name (I)
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
4、数组指针和指针数组
MathML to latex
Implement a server with multi process concurrency
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people