当前位置:网站首页>LeetCode_ String_ Simple_ 412.Fizz Buzz
LeetCode_ String_ Simple_ 412.Fizz Buzz
2022-07-02 15:15:00 【I'm up and down in the Jianghu】
1. subject
Give you an integer n , Find out from 1 To n Of each integer Fizz Buzz Express , And use string array answer( Subscript from 1 Start ) Return results , among :
answer[i] == “FizzBuzz”, If i At the same time 3 and 5 Multiple .
answer[i] == “Fizz”, If i yes 3 Multiple .
answer[i] == “Buzz”, If i yes 5 Multiple .
answer[i] == i ( In string form ), If none of the above conditions are met .
Example 1:
Input :n = 3
Output :[“1”,“2”,“Fizz”]
Example 2:
Input :n = 5
Output :[“1”,“2”,“Fizz”,“4”,“Buzz”]
Example 3:
Input :n = 15
Output :[“1”,“2”,“Fizz”,“4”,“Buzz”,“Fizz”,“7”,“8”,“Fizz”,“Buzz”,“11”,“Fizz”,“13”,“14”,“FizzBuzz”]
Tips :
1 <= n <= 104
source : Power button (LeetCode)
link :https://leetcode.cn/problems/fizz-buzz
2. Ideas
(1) simulation
The topic is relatively simple , have access to if-else Statement to judge each situation , And use answer To save the corresponding results .
3. Code implementation (Java)
// Ideas 1———— simulation
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;
}
}
边栏推荐
猜你喜欢
解决el-radio-group 回显后不能编辑问题
Data analysis thinking analysis methods and business knowledge - business indicators
08_ 串
forEach的错误用法,你都学废了吗
[email protected] : The platform “win32“ is incompatible with this module."/>
info [email protected] : The platform “win32“ is incompatible with this module.
06_ Stack and queue conversion
04_ 栈
21_ Redis_ Analysis of redis cache penetration and avalanche
. Net core logging system
19_Redis_宕机后手动配置主机
随机推荐
How does CTO help the business?
原则、语言、编译、解释
05_队列
牛客练习赛101
HUSTPC2022
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
LeetCode 209. 长度最小的子数组
如何用 Sysbench 测试 TiDB
IE 浏览器正式退休
Why can't programmers who can only program become excellent developers?
蜻蜓低代码安全工具平台开发之路
07_哈希
传感器数据怎么写入电脑数据库
Learn the method code example of converting timestamp to uppercase date using PHP
Tidb hybrid deployment topology
12_Redis_Bitmap_命令
PHP method to get the index value of the array item with the largest key value in the array
How to test tidb with sysbench
Base64 编码原来还可以这么理解
03_ Linear table_ Linked list