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 嘶嘶嗡嗡的更多相关文章

  1. Java实现 LeetCode 412 Fizz Buzz

    412. Fizz Buzz 写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz" ...

  2. LeetCode 412. Fizz Buzz

    Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...

  3. LeetCode: 412 Fizz Buzz(easy)

    题目: Write a program that outputs the string representation of numbers from 1 to n. But for multiples ...

  4. LeetCode 412 Fizz Buzz 解题报告

    题目要求 Write a program that outputs the string representation of numbers from 1 to n. But for multiple ...

  5. 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 ...

  6. 【leetcode】412. Fizz Buzz

    problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...

  7. [LeetCode] Fizz Buzz 嘶嘶嗡嗡

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

  8. 【LeetCode】412. Fizz Buzz 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 方法一:遍历判断 方法二:字符串相加 方法三:字典 日期 [L ...

  9. 力扣(LeetCode)412. Fizz Buzz

    写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...

随机推荐

  1. Codeforces_Round_547 (Div. 3)题解

    题目链接 传送门 A题 题目 题意 给你两个正整数\(n\)和\(m\),然后你可以进行无数次操作(每次操作可以将\(n\)扩大两倍,或者扩大三倍),问你是否能够得到\(m\). 代码实现如下 n, ...

  2. Jenkins - 扯淡篇

    目录 什么是持续集成 持续集成的概念 持续交付 持续部署 流程 当没有Jenkins的时候... 什么是Jenkins 返回Jenkins目录 什么是持续集成 由于懒得写,所以本段摘自阮一峰老师的博客 ...

  3. Zepto.js简介

    Zepto.js简介 一.总结 一句话总结: Zepto.js语法和jquery起码百分之90相似,主要做移动端框架,和jquery mobile是一个类型的概念 1.Zepto.js做移动端的特点? ...

  4. HDU - 5571 :tree (动态点分治 异或)

    题意:给定一棵树,有点权a[],有边权. 现在有M次修改点权的操作,输出每次修改后,Σ(a[i]^a[j])*dis(i,j); 思路:因为待修改,我们需要快速得到以及修改一个点到其他所有点的信息. ...

  5. sass中的占位符%,@extend,@mixin(@include)的编译区别和使用场景

    对于下面同一段css,它们的编译效率是不同的. 1.使用@extend:基础类icon会出现在编译后的css文件中,即使它可能只是拿来被继承,而不是作为icon这个class单独使用 //基础类ico ...

  6. tensorflow API _ 5 (tensorflow.summary)

    tensorflow的可视化是使用summary和tensorboard合作完成的. 基本用法 首先明确一点,summary也是op. 输出网络结构 with tf.Session() as sess ...

  7. robot framework的使用方法

    1.后台代码: 目录结构: 测试代码:Arithmetic.py 2.开始编写用例 直接在eclipse上新建一个txt文件即可,或者是通过ride编写用例. (1).首先在eclipse上新建目录T ...

  8. Spring之IOC(控制反转)与AOP(面向切面编程)

    控制反转——Spring通过一种称作控制反转(IoC)的技术促进了松耦合.当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象.可以认为IoC与JND ...

  9. Win32下的中断和异常

    本文是Matt Pietrek在1997年月10月的MSJ杂志Under The Hood专栏上发表的文章.中断和异常在DOS时代是整个系统的灵魂,但Windows已将其隐藏到了系统深处.Matt P ...

  10. 解决js加减乘除精度问题

    // 加法 const accAdd = (arg1, arg2) => {     var r1, r2, m;     try {         r1 = arg1.toString(). ...