【LeetCode-Python】412. Fizz Buzz
来源:互联网 发布:淘宝网鞋子女鞋春季 编辑:程序博客网 时间:2024/06/05 22:52
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”
]
简单解释:3的倍数位置替换为Fizz,5的倍数位置替换为Buzz,15的倍数位置替换为FizzBuzz
Solution1[Python]:
class Solution(object): def fizzBuzz(self, n): """ :type n: int :rtype: List[str] """ res = [] if n == 0: return res for i in range(n): if (i + 1) % 15 == 0: res.append('FizzBuzz') elif (i + 1) % 5 == 0: res.append('Buzz') elif (i + 1) % 3 == 0: res.append('Fizz') else: res.append(str(i+1)) return res
Solution2[Python]: just one line
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)]
0 0
- 【LeetCode-Python】412. Fizz Buzz
- [leetcode: Python]412.Fizz Buzz
- LeetCode 412. Fizz Buzz
- [LeetCode]--412. Fizz Buzz
- LeetCode-412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 【leetcode】412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- Leetcode 412.Fizz Buzz
- leetcode-412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 【LeetCode】 412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- LeetCode--412. Fizz Buzz
- 412. Fizz Buzz [LeetCode]
- [leetcode]--412. Fizz Buzz
- 过滤器
- VR
- 100天土鸡饲养计划(68)
- session创建
- SPA (单页应用程序)
- 【LeetCode-Python】412. Fizz Buzz
- 14、模板方法模式(Template Method)
- apue p194页 一个有意思 的demo
- Neither the JAVA_HOME nor the JRE_HOME environment variable is defined解决
- Android 轻松实现后台搭建+APP版本更新
- Java内存空间划分初识(适合初学者)
- Ubuntu 14.04 LTS 下升级 gcc 到 gcc-4.9、gcc-5 版本
- tcp转发器使用说明
- 在Android 5.0 SEAndroid下通过JNI访问一个内核节点