LeetCode Fizz Buzz
原题链接在这里:https://leetcode.com/problems/fizz-buzz/
题目:
Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
Example:
n = 15, Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
题解:
从1到n, 当前数能否被15, 5, 3整除,添加对应String. 均不能整除添加当前数.
Time Complexity: O(n). Space: O(1) regardless res.
AC Java:
public class Solution {
public List<String> fizzBuzz(int n) {
List<String> res = new ArrayList<String>();
for(int i = 1; i<=n; i++){
if(i%5 == 0 && i%3 == 0){
res.add("FizzBuzz");
}else if(i%5 == 0){
res.add("Buzz");
}else if(i%3 == 0){
res.add("Fizz");
}else{
res.add(String.valueOf(i));
}
}
return res;
}
}
LeetCode Fizz Buzz的更多相关文章
- LeetCode——Fizz Buzz
LeetCode--Fizz Buzz Question Write a program that outputs the string representation of numbers from ...
- [LeetCode] Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- LeetCode算法题-Fizz Buzz(Java实现)
这是悦乐书的第221次更新,第233篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第88题(顺位题号是412). 编写一个程序,输出从1到n的数字的字符串表示.但对于三的 ...
- [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- 【LeetCode】412. Fizz Buzz 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 方法一:遍历判断 方法二:字符串相加 方法三:字典 日期 [L ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- 【leetcode】412. Fizz Buzz
problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...
- Leetcode 414.Fizz Buzz By Python
写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...
- 力扣(LeetCode)412. Fizz Buzz
写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...
随机推荐
- userdel 连同家目录一起删除
userdel -r xxx 连同家目录一起删除
- 无废话ExtJs 入门教程二十[数据交互:AJAX]
无废话ExtJs 入门教程二十[数据交互:AJAX] extjs技术交流,欢迎加群(521711109) 1.代码如下: 1 <!DOCTYPE html PUBLIC "-//W3C ...
- Mac系统 安装SVN
- Linux第03天
Linux 第03天 1.Linux帐号和ACL权限管理 1.帐号和用户组 1.1 用户标识符————UID(root为0 系统用户为1~499 普通用户为500~65535) 1.2 用户组标识符— ...
- TextSwitcher 简单用例
TextSwitcher 字面理解是文字交换器,是ViewSwitcher的子类,从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,显然是交换T ...
- MT4平台经验总结
https://www.mql5.com/zh/code/8462 https://www.mql5.com/zh/code/8074 https://www.mql5.com/zh/code/787 ...
- NSDateFormatter 相关理解
Formatter译为格式,相应的NSDateFormatter就相当于是NSDate的转换类,将NSDate转换为另一种格式,或转换回来.NSDate没有自己的输出,需要借助NSDateFormat ...
- centos使用yum安装软件的时候出现了undefined symbol: CRYPTO_set_locking_callback
1.问题 在CentOS下使用yum安装软件,结果出现了下面的错误提示: # yum installThere was a problem importing one of the Python mo ...
- Docking Windows Phone controls to the bottom of a StackPanel
<Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinit ...
- 移动手机端H5无缝间歇平滑向上滚动js代码
在没结合css3的transform实现平滑过渡前,我都是用的jquery的animate方法,此方法在PC端基本看不出来有稍微卡顿的现象,但是在性能不高的手机上使用该方法,就会有明显的卡顿现象,不够 ...