原题链接在这里:https://leetcode.com/problems/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"
]

题解:

从1到n, 当前数能否被15, 5, 3整除,添加对应String. 均不能整除添加当前数.

Time Complexity: O(n). Space: O(1) regardless res.

AC Java:

 public class Solution {
public List<String> fizzBuzz(int n) {
List<String> res = new ArrayList<String>();
for(int i = 1; i<=n; i++){
if(i%5 == 0 && i%3 == 0){
res.add("FizzBuzz");
}else if(i%5 == 0){
res.add("Buzz");
}else if(i%3 == 0){
res.add("Fizz");
}else{
res.add(String.valueOf(i));
}
}
return res;
}
}

LeetCode Fizz Buzz的更多相关文章

  1. LeetCode——Fizz Buzz

    LeetCode--Fizz Buzz Question Write a program that outputs the string representation of numbers from ...

  2. [LeetCode] Fizz Buzz 嘶嘶嗡嗡

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

  3. LeetCode算法题-Fizz Buzz(Java实现)

    这是悦乐书的第221次更新,第233篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第88题(顺位题号是412). 编写一个程序,输出从1到n的数字的字符串表示.但对于三的 ...

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

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

  5. 【LeetCode】412. Fizz Buzz 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 方法一:遍历判断 方法二:字符串相加 方法三:字典 日期 [L ...

  6. LeetCode 412. Fizz Buzz

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

  7. 【leetcode】412. Fizz Buzz

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

  8. Leetcode 414.Fizz Buzz By Python

    写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...

  9. 力扣(LeetCode)412. Fizz Buzz

    写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...

随机推荐

  1. NYOJ题目28大数阶乘

    -------------------------------------祭出BigInteger AC代码: import java.math.BigInteger; import java.uti ...

  2. redis 密码配置

    http://blog.csdn.net/vtopqx/article/details/46833099 http://www.2cto.com/database/201412/365757.html ...

  3. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...

  4. setTimeout使用闭包功能,实现定时打印数值

    我们这次使用setTimeout来实现一个按照时间定时,依次打印数值的例子.其实在早期的时候,也是我经常犯的一个错误,或者实现这种能力,似乎js比较牵强,其实是我的错,哈哈!没能理解JS强大之处.我们 ...

  5. 详解ABP框架的多租户

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:ABP框架对多租户场景提供了很好的支持,内建了多租户的处理机制,今天我们来深入解析一下 ...

  6. 在编译命令行中添加 /D_SCL_SECURE_NO_DEPRECATE

    问题:Add the option /D_SCL_SECURE_NO_DEPRECATE to the compilation command 解决方案:项目属性 –> 配置属性 –> C ...

  7. MyBabis 用法详解

    MyBatis 一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架(消除了几乎所以得JDBC代码和参数的手工设置及结果集的检索) MyBatis可以使用简单的XML或注解用于配置和原始映射,将 ...

  8. js最佳继承范型

    先回想下怎么给一个类设置属性:1.构造函数 内  通过this2.prototype中的属性两者的区别就是构造函数中的属性是每个实例私有的,而prototype中的属性是所有实例共有的(一般方法和静态 ...

  9. 如何直接在ftp里编辑文件

    首先要连接ftp,如何链接ftp,这个我已经分享过,如果还有不懂的,可以查看下经验分享,打开ftp,并连接你要修改的站点!   下载安装代码编辑器,比如EditPlus.sublime text等,随 ...

  10. C#_Express-ickd接口

    爱查快递接口使用 using System; using System.Collections.Generic; using System.IO; using System.Net; using Sy ...