9. Fizz Buzz 问题
Description
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 3 and 5, print "fizz buzz".
Example
If n = 15
, you should return:
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz", "11", "fizz",
"13", "14", "fizz buzz"
]
Challenge
Can you do it with only one if statement?
public class Solution {
/**
* @param n: An integer
* @return: A list of strings.
*/
public List<String> fizzBuzz(int n) {
// write your code here
List<String> res = new ArrayList<>();
for(int i=1;i<n+1;i++){
if(i % 15 == 0){
res.add("fizz buzz");
}else if( i % 3 ==0){
res.add("fizz");
}else if( i % 5 == 0){
res.add("buzz");
}else{
res.add(i + "");
}
}
return res;
}
}
描述
给你一个整数n. 从 1 到 n 按照下面的规则打印每个数:
如果这个数被3整除,打印fizz.
如果这个数被5整除,打印buzz.
如果这个数能同时被3和5整除,打印fizz buzz.
您在真实的面试中是否遇到过这个题?
样例
比如 n = 15, 返回一个字符串数组:
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz", "11", "fizz",
"13", "14", "fizz buzz"
]
9. Fizz Buzz 问题的更多相关文章
- [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> ...
- [重构到模式-Chain of Responsibility Pattern]把Fizz Buzz招式重构到责任链模式
写一段程序从1打印到100,但是遇到3的倍数时打印Fizz,遇到5的倍数时打印Buzz,遇到即是3的倍数同时也是5的倍数时打印FizzBuzz.例如: 1 2 Fizz 4 Buzz Fizz 7 8 ...
- Swift完成fizz buzz test
看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的fizz buzz测试都完不成. 不知道fizz buzz test为何物的,建议自行搜之. 测试要求是,编写满足以下条件的代码: Write ...
- [Swift]LeetCode412. Fizz Buzz
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- Fizz Buzz 面试题
在CSDN上看到一篇文章<软件工程师如何笑着活下去>,本来是想看对这个行业的一些评价和信息,不曾检索到关于Fizz Buzz的面试题,上网搜了一下,顿感兴趣.留下此文,以表回忆. java ...
随机推荐
- 【git】提交代码到远程仓库
看完不用,就是一个字:忘! 之前学了两天git结果今天要用的时候,啥也想不起来.... 场景: 已有远程仓库: git@192.168.1.1:test/test.git 要提交代码到远程仓库的新分支 ...
- JSP 隐式对象
一. 隐式对象 JSP隐式对象 对象 类型 request javax.servlet.http.HttpServletRequest response javax.servlet.http.Http ...
- jquery----Ajax补充
jquery实现ajax请求 <script> //$.ajax的两种使用方式: //$.ajax(settings); //$.ajax(url,[settings]); $(" ...
- Java 11 这 8 个逆天新特性教你写出更牛逼的代码!
美国时间2018年 09 月 25 日,Oralce 正式发布了 Java 11,这是据 Java 8 以后支持的首个长期版本. 为什么说是长期版本,看下面的官方发布的支持路线图表. 可以看出 Jav ...
- C# 不使用递归遍历目录树中的文件和文件夹
public class StackBasedIteration { static void Main(string[] args) { // Specify the starting folder ...
- 利用SVD-推荐未尝过的菜肴2
推荐未尝过的菜肴-基于SVD的评分估计 实际上数据集要比我们上一篇展示的myMat要稀疏的多. from numpy import linalg as la from numpy import * d ...
- python 内建函数
# # __geratteibute__class Itcast(object): def __init__(self,subject1): self.subject1 = subject1 self ...
- python no module named builtins
使用python2时出现此错误. 解决办法: pip install future
- 安装淘宝npm(cnpm)
https://www.cnblogs.com/yominhi/p/7039795.html http://npm.taobao.org/ 初始化一个 mpvue 项目 现代前端开发框架和环境都是需要 ...
- 阿里 Goldeneye 四个环节落地智能监控:预测、检测、报警及定位
阿里 Goldeneye 四个环节落地智能监控:预测.检测.报警及定位 https://www.infoq.cn/article/alibaba-goldeneye-four-links