[LeetCode] 412. 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的每一个数对3,5取模,根据情况写入结果。然后就是有一些极简的写法和思路,可以看看大牛们的写法,也挺受用的。
Java: Not use '%' operation
public class Solution {
public List<String> fizzBuzz(int n) {
List<String> ret = new ArrayList<String>(n);
for(int i=1,fizz=0,buzz=0;i<=n ;i++){
fizz++;
buzz++;
if(fizz==3 && buzz==5){
ret.add("FizzBuzz");
fizz=0;
buzz=0;
}else if(fizz==3){
ret.add("Fizz");
fizz=0;
}else if(buzz==5){
ret.add("Buzz");
buzz=0;
}else{
ret.add(String.valueOf(i));
}
}
return ret;
}
}
Java:
public class Solution {
public List<String> fizzBuzz(int n) {
List<String> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) {
list.add("FizzBuzz");
} else if (i % 3 == 0) {
list.add("Fizz");
} else if (i % 5 == 0) {
list.add("Buzz");
} else {
list.add(String.valueOf(i));
}
}
return list;
}
}
Python:
def fizzBuzz(self, n):
return ['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, n+1)]
Python:
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
return [str(i) if (i%3!=0 and i%5!=0) else (('Fizz'*(i%3==0)) + ('Buzz'*(i%5==0))) for i in range(1,n+1)]
Python:
def fizzBuzz(self, n):
return ['FizzBuzz'[i % -3 & -4:i % -5 & 8 ^ 12] or repr(i) for i in range(1, n + 1)]
Python:
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
result = []
for i in xrange(1, n+1):
if i % 15 == 0:
result.append("FizzBuzz")
elif i % 5 == 0:
result.append("Buzz")
elif i % 3 == 0:
result.append("Fizz")
else:
result.append(str(i)) return result
Python:
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
l = [str(x) for x in range(n + 1)]
l3 = range(0, n + 1, 3)
l5 = range(0, n + 1, 5)
for i in l3:
l[i] = 'Fizz'
for i in l5:
if l[i] == 'Fizz':
l[i] += 'Buzz'
else:
l[i] = 'Buzz'
return l[1:]
Python: wo
class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
for i in xrange(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
res.append('FizzBuzz')
elif i % 3 == 0:
res.append('Fizz')
elif i % 5 == 0:
res.append('Buzz')
else:
res.append(str(i)) return res
C++:
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> res;
for (int i = 1; i <= n; ++i) {
if (i % 15 == 0) res.push_back("FizzBuzz");
else if (i % 3 == 0) res.push_back("Fizz");
else if (i % 5 == 0) res.push_back("Buzz");
else res.push_back(to_string(i));
}
return res;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡的更多相关文章
- Java实现 LeetCode 412 Fizz Buzz
412. Fizz Buzz 写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz" ...
- 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(easy)
题目: Write a program that outputs the string representation of numbers from 1 to n. But for multiples ...
- LeetCode 412 Fizz Buzz 解题报告
题目要求 Write a program that outputs the string representation of numbers from 1 to n. But for multiple ...
- LeetCode - 412. Fizz Buzz - ( C++ ) - 解题报告 - to_string
1.题目大意 Write a program that outputs the string representation of numbers from 1 to n. But for multip ...
- 【leetcode】412. Fizz Buzz
problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...
- [LeetCode] 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
写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...
随机推荐
- 【原创】selenium+python+openpyxl实现登录自动化测试,自动读取excel用例数据,并将数据结果自动写入到excel
# -*- coding: utf-8 -*- from selenium import webdriver from openpyxl import load_workbook from time ...
- 项目Alpha冲刺——集合
作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 完成项目Alpha冲刺 团队信息 队名:火鸡堂 队员学号 队员姓名 博客地址 ...
- danci1
oddball 英 ['ɒdbɔːl] 美 adj. 古怪的:奇怪的 n. 古怪:古怪的人 rather than 英 美 而不是:宁可…也不愿 grasp 英 [grɑːsp] 美 [ɡræsp] ...
- 使用 Word 写作论文时设置格式技巧记录
这里主要记录使用 Word 2013 版本的 Microsoft office Word 软件进行论文书写时的一些常用的格式设置技巧,以供分享与记录. Word文档页脚添加页码 Word设置多级标题格 ...
- 性能:Output层面
将数据保存到MySQL中 import java.sql.DriverManager import org.apache.spark.storage.StorageLevel import org.a ...
- 002_Visual Studio (gnuplot)显示数组波形
视频教程:https://v.qq.com/x/page/e3039v4j6zs.html 资料下载:https://download.csdn.net/download/xiaoguoge11/12 ...
- jaeger 使用scylladb作为后端存储
scylladb 是一个不错的apache Cassandra 替代,而且兼容很不错,今天在尝试过yugabyte 之后放弃了,因为在进行jaeger 创建 Cassandra schema 的时候碰 ...
- fake_useragent 本地运行各种报错解决办法
- 【JZOJ6226】【20190618】纳什均衡
题目 一颗二叉树,每个点儿子个数为0 或 2 ,对每个叶子有一个权值\((c(u),d(u))\) 从根结点开始走,Alice 可以选择奇数层的走法,Bob 可以选择偶数层的走法,分别获得最后走到叶子 ...
- PHP 打印输出数组内容及结构 print_r 与 var_dump 函数
利用 print_r() 函数可以打印输出整个数组内容及结构,按照一定格式显示键和元素.注意 print_r() 函数不仅是只用于打印,实际它是用于打印关于变量的易于理解的信息. 例子1 <?p ...