LeetCode 412. Fizz Buzz

LeetCode 412. Fizz Buzz

Description

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-8 -*-
# @Author:             何睿
# @Create Date:        2019-10-05 20:01:30
# @Last Modified by:   何睿
# @Last Modified time: 2019-10-05 20:02:43


from typing import List


class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        return ['FizzBuzz' if not i % 15 else 'Buzz' if not i % 5 else 'Fizz' if not i % 3 else str(i) for i in range(1, n + 1)]

©本文是原创文章,欢迎转载,转载需保留 文章来源 ,作者信息和本声明.

Posted in 力扣 and tagged , .

花若盛開,蝴蝶自來. 分享学习,每天进步一点点

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据