当前位置:网站首页>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;
}
}
边栏推荐
- Daily learning 3
- Fabric.js 手动加粗文本iText
- Fundamentals of software testing
- 用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
- Yolov6 training: various problems encountered in training your dataset
- [Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
- The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
- YoloV6训练:训练自己数据集遇到的各种问题
- ##51单片机实验之简易验证码发生器
- 删除元素(带过渡动画)
猜你喜欢
Fabric.js 橡皮擦的用法(包含恢复功能)
obsidian安装第三方插件——无法加载插件
Development and design of animation surrounding mall sales website based on php+mysql
Wechat applet uses towxml to display formula
Fabric. JS zoom canvas
复用和分用
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
buuctf-pwn write-ups (7)
The most complete analysis of Flink frame window function
mathjax 入门(web显示数学公式,矢量的)
随机推荐
测试框架TestNG的使用(二):testNG xml的使用
Chapter 9: xshell free version installation
1. Editing weapon VIM
Find the maximum inscribed circle of the contour
JMeter script parameterization
Yolov6 training: various problems encountered in training your dataset
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
MQ教程 | Exchange(交换机)
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
uniapp自动化测试学习
由粒子加速器产生的反中子形成的白洞
Actual combat sharing of shutter screen acquisition
[QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
大顶堆、小顶堆与堆排序
mongodb的认识
使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
C语言高级用法--函数指针:回调函数;转换表
Fabric.js 自由绘制椭圆
freemarker的使用
Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control