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> ...
随机推荐
- qt5 移植 交叉编译出现错误
类似这样的错误,当时没有完整的记下来,undefined reference to `std::__detail::_List_node_base@GLIBCXX_3.4.10 当时是在编译qt5cl ...
- UART接口与COM口的区别
原文地址:https://blog.csdn.net/wordwarwordwar/article/details/78883732 简单的讲:(UART与COM) 嵌入式里面说的串口,一般是指UAR ...
- [转载]Cookie与Session的区别与联系及生命周期
前几天面试问了一个问题,当时记不太清了,上网查了下发现这个问题还真的很有讲究而且很重要,自己总结下做下记录. 一.Session与Cookie介绍 这些都是基础知识,不过有必要做深入了解.先简单介绍一 ...
- 如何使用Wisdom RESTClient定制满足您个性化需求的API文档?
Wisdom RESTClient 支持自动化测试RESTful API,输出精美的测试报告,生成精美的RESTful API文档. 这里介绍一下如何定制个性化的RESTful API文档. 定制个性 ...
- Censor SCU - 4438
frog is now a editor to censor so-called sensitive words (敏感词). She has a long text (p). Her job is ...
- HFA and outhandler differentiate or not
better trouble shooting auto-open accidently? HFA not stable? check code modification ASAP!!!
- Linux下php添加memcache扩展
很多时候我们都会遇到在已经安装的php中添加其它的扩展. 那我们应该怎么做呢? 这样做.(我们的nginx和php都是已经安装好了的,这里就不做赘述了) 首先,我们需要下载php的memcache扩展 ...
- linux下nginx编译安装
步骤: 1.获取nginx安装包. 进入nginx官网:http://nginx.org/ 找到稳定版本: 点击红框内的链接. 使用wget获取安装包. wget http://nginx.org/d ...
- DOM EventListener
向元素添加事件句柄的语法:element.addEventListener(event, function, useCapture); 第一个参数是事件的类型,如click或者mousedown,注意 ...
- SQL介绍
SQL,即structured query language,结构化查询语言,是一种对关系型数据库中的数据进行管理和操作的语言方法,SQL包括6个部分 DQL:数据查询语言,最常用的为select,其 ...