当前位置:网站首页>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;
}
}
边栏推荐
- 复用和分用
- A white hole formed by antineutrons produced by particle accelerators
- Fabric. JS free draw circle
- tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
- Uniapp automated test learning
- 字符串匹配问题
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
- taobao. trade. Get (get some information of a single transaction), Taobao store order interface, Taobao oauth2.0 interface, Taobao R2 interface code docking and sharing
- 4、数组指针和指针数组
- Wechat applet uses towxml to display formula
猜你喜欢

Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.

mathjax 入门(web显示数学公式,矢量的)

Fabric.js 自由绘制圆形

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

Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?

报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

MathML to latex

由粒子加速器产生的反中子形成的白洞

Xilinx Vivado set *. svh as SystemVerilog Header

mathML转latex
随机推荐
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
Ad20 cannot select the solution of component packaging in PCB editor
Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
Fabric. JS free draw circle
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
STM32标准固件库函数名记忆(二)
Method of creating linked server for cross server data access
YoloV6训练:训练自己数据集遇到的各种问题
Fatal: unsafe repository is owned by someone else
1、编辑利器vim
Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
Find the maximum inscribed circle of the contour
【apipost】使用教程
报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
Fabric.js 缩放画布
STM32标准固件库函数名(一)
检查密码
Why can't browsers read JSX?
数据库连接池和数据源
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?