Lintcode9-Fizz Buzz-Easy
Fizz Buzz
Given number n. Print number from 1 to n. But:
- when number is divided by
3, print"fizz". - when number is divided by
5, print"buzz". - when number is divided by both
3and5, print"fizz buzz". - when number can't be divided by either
3or5, print the numberitself.
Example
If n = 15, you should return:
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz", "11", "fizz",
"13", "14", "fizz buzz"
]
If n = 10, you should return:
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz"
]
Challenge
Can you do it with only one if statement?
注意:
- 同时被3和5整除,应该是 num % 15 == 0 , 而不是 nums % 3 == 0 && nums % 5 == 0 , 后者会加长运行时间。
- 四种情况的判断顺序很重要,同样影响运行时间。采用 "if - else if - else if - else": if (num % 15 == 0)- else if (num % 5 == 0) - else if (num % 3 == 0) - else. else if 对判断先后顺序有要求,不可颠倒。而如果全部是if, 每个if判断的条件会增加,即增加运行时间。if (num % 15 == 0)- if (num % 5 == 0 && num % 3 != 0) -if (num % 3 == 0 && num % 5 != 0) - else.
- Java int -> String : String s = String.valueOf(i);
- List.add() 参数只能是字符串,所以 line 13.
代码:
public List<String> fizzBuzz(int n) {
int num = 1;
List<String> fblist = new ArrayList<String>();
while (num <= n){
if (num % 15 == 0) {
fblist.add("fizz buzz");
} else if (num % 5 == 0) {
fblist.add("buzz");
} else if (num % 3 == 0) {
fblist.add("fizz");
} else {
fblist.add(String.valueOf(num));
}
num++;
}
return fblist;
}
Lintcode9-Fizz Buzz-Easy的更多相关文章
- LeetCode: 412 Fizz Buzz(easy)
题目: Write a program that outputs the string representation of numbers from 1 to n. But for multiples ...
- LeetCode_412. Fizz Buzz
412. Fizz Buzz Easy Write a program that outputs the string representation of numbers from 1 to n. B ...
- LeetCode算法题-Fizz Buzz(Java实现)
这是悦乐书的第221次更新,第233篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第88题(顺位题号是412). 编写一个程序,输出从1到n的数字的字符串表示.但对于三的 ...
- 【LeetCode】412. Fizz Buzz 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 方法一:遍历判断 方法二:字符串相加 方法三:字典 日期 [L ...
- [LeetCode] Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- Lintcode 9.Fizz Buzz 问题
------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of s ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- LeetCode Fizz Buzz
原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...
- Fizz Buzz
class Solution { public: /** * param n: As description. * return: A list of strings. */ vector<st ...
- LintCode (9)Fizz Buzz
下面是AC代码,C++风格: class Solution { public: vector<string> fizzBuzz(int N) { vector<string> ...
随机推荐
- restful的特点
1. 资源(Resources) REST的名称”表现层状态转化”中,省略了主语.”表现层”其实指的是”资源”(Resources)的”表现层”. 所谓”资源”,就是网络 ...
- sql server启动服务和还原bak文件
sql server启动服务和还原bak文件, sql server启动要: mysql数据库备份是psc后缀文件, sql server还原数据库备份bak文件: 三张图简介明了: ok:
- Linux基础命令---显示域名ypdomainname
ypdomainname ypdomainname指令显示由函数“getdomainname”返回的主机域名,使用这个指令也可以设置一个主机NIS/YP域名. 此命令的适用范围:RedHat.RH ...
- Linux基础命令---调整程序优先级renice
renice renice指令可以重新调整程序运行的优先级,可以通过进程id.用户id.组id来修改优先级.修改组的等级,影响组内所有用户的所有进程优先级:修改用户等级,影响该用户的所有进程优先级.除 ...
- Linux下几种重启Nginx的方式,找出nginx配置文件路径和测试配置文件是否正确
Linux下几种重启Nginx的方式,找出nginx配置文件路径和测试配置文件是否正确 目录在/etc/ngnix/conf.d下找出nginx配置文件路径和测试配置文件是否正确# /usr/sbin ...
- pytest+request 接口自动化测试
1.安装python3brew update brew install pyenv 然后在 .bash_profile 文件中添加 eval “$(pyenv init -)” pyenv insta ...
- VMware激活密钥
VMware 2017 v14.x 永久许可证激活密钥FF31K-AHZD1-H8ETZ-8WWEZ-WUUVACV7T2-6WY5Q-48EWP-ZXY7X-QGUWD 原文链接
- mybatis总结之一
今日内容:mybatis数据持久层的一种表现,在一定程度上,取代了jdbc. 1.建maven...... 在pom.xml中进行配置,添加mabatis包,junit测试jar包,添加连接mysql ...
- mysql用户的增删与密码丢失问题
为root用户设置初始密码 mysqladmin -u root password 密码(单实例) mysqladmin -u root password 密码 -S /data/3306/mysql ...
- P2564 [SCOI2009]生日礼物(尺取法)
P2564 [SCOI2009]生日礼物 三个字.尺取法......... 坐标按x轴排序. 蓝后尺取一下.......... #include<iostream> #include< ...