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 问题的更多相关文章

  1. [LeetCode] Fizz Buzz 嘶嘶嗡嗡

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

  2. Lintcode 9.Fizz Buzz 问题

    ------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of s ...

  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 Fizz Buzz

    原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...

  5. Fizz Buzz

    class Solution { public: /** * param n: As description. * return: A list of strings. */ vector<st ...

  6. LintCode (9)Fizz Buzz

    下面是AC代码,C++风格: class Solution { public: vector<string> fizzBuzz(int N) { vector<string> ...

  7. [重构到模式-Chain of Responsibility Pattern]把Fizz Buzz招式重构到责任链模式

    写一段程序从1打印到100,但是遇到3的倍数时打印Fizz,遇到5的倍数时打印Buzz,遇到即是3的倍数同时也是5的倍数时打印FizzBuzz.例如: 1 2 Fizz 4 Buzz Fizz 7 8 ...

  8. Swift完成fizz buzz test

    看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的fizz buzz测试都完不成. 不知道fizz buzz test为何物的,建议自行搜之. 测试要求是,编写满足以下条件的代码: Write ...

  9. [Swift]LeetCode412. Fizz Buzz

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

  10. Fizz Buzz 面试题

    在CSDN上看到一篇文章<软件工程师如何笑着活下去>,本来是想看对这个行业的一些评价和信息,不曾检索到关于Fizz Buzz的面试题,上网搜了一下,顿感兴趣.留下此文,以表回忆. java ...

随机推荐

  1. 《剑指offer》 链表中倒数第k个节点

    本题来自<剑指offer> 链表中倒数第k个节点 题目: 输入一个链表,输出该链表中倒数第k个结点. 思路: 倒数第k个节点,而且只能访问一遍链表,定义两个节点,两者之间相差k个距离,遍历 ...

  2. WEB测试总结

    WEB测试总结:1.js文件session是否有缓存,如果没有缓存对服务器压力会很大:2.更改页面大小后,图表是否会再次向服务器请求数据:3.表单填写是否对html标识,script脚本做处理:(&l ...

  3. 首次使用idea步骤

    修改代码提示快捷键 默认是ctrl+空格,这个会跟中英文切换的快捷键冲突. 我这里改成了alt+/ tomcat的配置

  4. ready()事件;使外置JS代码正常运行

    JavaScript代码放在哪里? 浏览器在渲染HTML页面时,是从头到尾,一行一行地检查执行的.如果JavaScript代码在前面,HTML元素在后面,遇到JavaScript选择一个还未渲染的HT ...

  5. PAT Basic 1071. 小赌怡情(15)

    题目内容 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注t个筹码后,计算机给出第二个数.若玩家猜对 ...

  6. js/jquery如何获取获取父窗口的元素

    1.$("#父窗口元素ID",window.parent.document); 对应javascript版本为 window.parent.document.getElementB ...

  7. Note for "Some Remarks on Writing Mathematical Proofs"

    John M. Lee is a famous mathematician, who bears the reputation of writing the classical book " ...

  8. 【转】ArcGIS10的附件功能

    转自:http://blog.csdn.net/linghe301/article/details/6386176 老是忘记怎么使用这个ArcGIS10的附件功能,这次就做个记录吧. 在项目应用过程中 ...

  9. ArcGIS 10开发迁移策略(待续)

    1.更改 ESRI.ArcGIS.ADF 程序集 ArcGIS 10 中, ADF 程序集中的功能被分散到不同的程序集中,如果将 ArcGIS 9.3 下 开发的自定义组件迁移到 ArcGIS 10 ...

  10. 使用impala对kudu进行DML操作

    将数据插入 Kudu 表 impala 允许使用标准 SQL 语句将数据插入 Kudu 插入单个值 创建表: CREATE TABLE my_first_table ( id BIGINT, name ...