当前位置:网站首页>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;
}
}
边栏推荐
- Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
- Tidb data migration scenario overview
- TiDB数据迁移工具概览
- Leetcode - Search 2D matrix
- info [email protected] : The platform “win32“ is incompatible with this module.
- TiDB 集群最小部署的拓扑架构
- The past and present lives of visual page building tools
- N皇后问题的解决
- CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
- [development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
猜你喜欢

LeetCode 209. Minimum length subarray

btrace-(字节码)动态跟踪工具

The past and present lives of visual page building tools

GeoServer offline map service construction and layer Publishing

08_ 串

LeetCode 209. 长度最小的子数组

About text selection in web pages and counting the length of selected text

14_Redis_乐观锁

04_ Stack

Mavn builds nexus private server
随机推荐
IE 浏览器正式退休
C language exercises - (array)
数据分析思维分析方法和业务知识——业务指标
05_ queue
记一次报错解决经历依赖重复
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
Huawei interview question: no palindrome string
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
C语言中的printf函数和scanf函数
Dragonfly low code security tool platform development path
List集合&UML图
使用 TiUP 部署 TiDB 集群
. Net core logging system
学习使用php实现公历农历转换的方法代码
CTO如何帮助业务?
How does the computer set up speakers to play microphone sound
How to conduct TPC-C test on tidb
Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
How to test tidb with sysbench
C语言实现N皇后问题