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"
]

  自己答案:

class Solution(object):
         def fizzBuzz(self, n):
      a = []
      i = 1
      while(i <= n):
        if(i%15 == 0):
          a.append("FizzBuzz")

        elif(i%3 == 0):
          a.append("Fizz")
        elif(i%5 == 0):
          a.append("Buzz")

        else:
          a.append(str(i))
        i = i + 1
      return a

  经典答案:

1.

class Solution(object):
def fizzBuzz(self, n):
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)]

2.

class Solution(object):
def fizzBuzz(self, n):
return["Fizz"*(not i%3) + "Buzz"*(not i %5) or str(i) for i in range(1,n+1)]

自己的答案没有考虑到列表生成式,没有发挥出Python特点:优雅、简介

列表生成式资料:

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00138681963899940a998c0ace64bb5ad45d1b56b103c48000

 

class Solution(object): def fizzBuzz(self, n): a = [] i = 1 while(i <= n): if(i%15 == 0): a.append("FizzBuzz") elifleetcode day_01的更多相关文章

  1. [LeetCode]题解(python):017-Letter Combinations of a Phone Number

    题目来源: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题意分析: 这道题是输入一段数字字符digits, ...

  2. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  3. 412. Fizz Buzz

    https://leetcode.com/problems/fizz-buzz/ 没什么好说的,上一个小学生解法 class Solution(object): def fizzBuzz(self, ...

  4. [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡

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

  5. python leetcode 1

    开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...

  6. Python字符串倒序-7. Reverse Integer

    今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...

  7. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  8. Leetcode5:Longest Palindromic Substring@Python

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  9. leetcode-Count Primes 以及python的小特性

    题目大家都非常熟悉,求小于n的所有素数的个数. 自己写的python 代码老是通不过时间门槛,无奈去看了看大家写的code.下面是我看到的投票最高的code: class Solution: # @p ...

随机推荐

  1. Java面向对象 第一章 面向对象开发方法概述

    一.软件开发经历的生命周期: ①软件分析 ②软件设计 ③软件编码 ④ 软件测试 ⑤ 软件部署 ⑥软件维护 二.为了提高软件开发效率,降低软件开发成本,一个优良的软件系统应该具备以下特点: ① 可重用性 ...

  2. u3d_Shader_effects笔记6 第二章 animating sprite

    1.前面的心情 上班看shader我也是醉了.写完这篇看代码去了,不过看着看着恐怕就会困.... 还有就是上天,我该怎么做,下一步,大懒: 2.参考源头 http://blog.csdn.net/ca ...

  3. 选中多个<ul>中的第一个<li>方法

    获取第一个ul中的第一个li标签的方法: //获取第一个ul中的第一个li /* $("ul li:first").css("background"," ...

  4. php 时间加减

    <?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...

  5. 15-前端开发之JavaScript

    什么是 JavaScript ? JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出 ...

  6. JSP中ResultSet的方法

    1,如何获得ResultSet的结构 ResultSetMetaData rsmd=rs.getMetaData();cn=rsmd.getColumnCount();for(int ik=1;ik& ...

  7. DayPilot 7.8 DLL去DEMO字样下载

    来自 DayPilot 的 7.8.3169.1 版本的DLL,微调去掉了“DEMO”字样,供参考,商用请支持正版! 此处下载: http://files.cnblogs.com/files/pcca ...

  8. MongoDB的基本操作

    环境:CentOS6.8  Mongodb3.2.10 启动 启动mongoDB服务器 # service mongod start 启动mongoDB客户端 # mongo 该客户端是一个JavaS ...

  9. BZOJ 1503: [NOI2004]郁闷的出纳员

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 10526  Solved: 3685[Submit][Stat ...

  10. Map工具系列-01-Map代码生成工具说明

    所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...