写一个程序,输出从 1 到 n 数字的字符串表示。

  1. 如果 n 是3的倍数,输出“Fizz”;

  2. 如果 n 是5的倍数,输出“Buzz”;

3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。

示例:

n = 15,

返回:

[

"1",

"2",

"Fizz",

"4",

"Buzz",

"Fizz",

"7",

"8",

"Fizz",

"Buzz",

"11",

"Fizz",

"13",

"14",

"FizzBuzz"

]

Java版

class Solution {
public List<String> fizzBuzz(int n) {
List<String> list = new LinkedList<>();
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(""+i);
}
}
return 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 嘶嘶嗡嗡

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

  3. LeetCode 412. Fizz Buzz

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

  4. LeetCode: 412 Fizz Buzz(easy)

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

  5. LeetCode 412 Fizz Buzz 解题报告

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

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

  7. 【leetcode】412. Fizz Buzz

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

  8. 力扣Leetcode 179. 最大数 EOJ 和你在一起 字符串拼接 组成最大数

    最大数 力扣 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说 ...

  9. 力扣Leetcode 45. 跳跃游戏 II - 贪心思想

    这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...

随机推荐

  1. Golang字符串函数认识(一)

    package main import ( "fmt" "strings" "strconv" ) func main(){ //返回字符串 ...

  2. yield表达式形式

    首先了解 1.iterator iterator叫做迭代器,用来遍历可以序列化的数据,比如一个list,set 等,当然如果对象想要能够使用迭代器来遍历,只要在该对象的类中添加__iter__()方法 ...

  3. android 颜色值参考,(有颜色图

    ) 2011-10-13 19:55:30| 分类: android | 标签:android颜色值|字号大中小 订阅 Android 常用RGB值以及中英文名称   颜 色 RGB值 英文名 中文名 ...

  4. 02: CMDB设计思路

    1.1 cmdb理解   参考博客:https://www.cnblogs.com/laowenBlog/p/6825420.html   参考博客2:https://www.cnblogs.com/ ...

  5. poj 3744 Scout (Another) YYF I - 概率与期望 - 动态规划 - 矩阵快速幂

      (Another) YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into th ...

  6. xlrd、xlwt 操作excel表格详解

    转自:https://www.cnblogs.com/jiablogs/p/9141414.html python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是 ...

  7. 【修改密码】Linux下修改Mysql的用户(root)的密码

    修改的用户都以root为列.一.拥有原来的myql的root的密码: 方法一:在mysql系统外,使用mysqladmin# mysqladmin -u root -p password " ...

  8. P3455 [POI2007]ZAP-Queries(莫比乌斯反演)

    思路 和YY的GCD类似但是更加简单了 类似的推一波公式即可 \[ F(n)=\sum_{n|d}f(d) \] \[ f(n)=\sum_{n|d}\mu(\frac{d}{n})F(d) \] \ ...

  9. LOJ6283 数列分块入门7(分块)

    pushdown的addtag[x]打成addtag[i],结果WA了一次 #include <cstdio> #include <algorithm> #include &l ...

  10. 利用Python 脚本生成 .h5 文件 代码

    利用Python 脚本生成 .h5 文件 import os, json, argparse from threading import Thread from Queue import Queue ...