412. Fizz Buzz
来源:互联网 发布:剑网三曹雪阳脸型数据 编辑:程序博客网 时间:2024/06/05 05:23
one day one cannon
or
one week five cannons
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"]
#coding=utf-8class Solution(object): def fizzBuzz(self,n): result=[] for i in range(1,n+1): if i%3==0 and i%5==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 def fizzBuzz1(self,n): return ['Fizz'*(not i % 3)+'Buzz'*(not i%5) or str(i) for i in range(1,n+1)]X=Solution()print X.fizzBuzz(20)print X.fizzBuzz1(20)
主要讲一下列表生成式。http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00138681963899940a998c0ace64bb5ad45d1b56b103c48000
return ['Fizz'*(not i % 3)+'Buzz'*(not i%5) or str(i) for i in range(1,n+1)]这句代码比较好,列表生成式, 字符串可以*bool值,也可以*整型数值
>>> "abd"*0''>>> 66>>> 'abd'*(not 6%3)'abd'>>> not (1)False>>> not 1False>>> 'abd'*True'abd'>>>>>> 'abd'*6'abdabdabdabdabdabd'
0 0
- 412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 412. Fizz Buzz
- 412. Fizz Buzz
- [LeetCode]--412. Fizz Buzz
- 412. Fizz Buzz
- LeetCode-412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 412. Fizz Buzz
- 412. Fizz Buzz
- 【leetcode】412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 412. Fizz Buzz
- 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- Leetcode 412.Fizz Buzz
- leetcode-412. Fizz Buzz
- hdu4135 Co-prime(容斥原理两种写法)
- 各种距离的相似性度量
- 组合数学中字典序法C++实现
- 判断一个数是否为2的N次方
- Android几种Service常驻内存的小思路
- 412. Fizz Buzz
- 三十而立
- markdown语法学习笔记
- GDKOI2017总结
- 1191
- 前端筆記
- linux进程间通讯
- 多线程编程
- 纯js实现html转pdf