412. Fizz Buzz

Easy

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"
]
package leetcode.easy;

public class FizzBuzz {
public java.util.List<String> fizzBuzz1(int n) { // ans list
java.util.List<String> ans = new java.util.ArrayList<String>(); for (int num = 1; num <= n; num++) { boolean divisibleBy3 = (num % 3 == 0);
boolean divisibleBy5 = (num % 5 == 0); if (divisibleBy3 && divisibleBy5) {
// Divides by both 3 and 5, add FizzBuzz
ans.add("FizzBuzz");
} else if (divisibleBy3) {
// Divides by 3, add Fizz
ans.add("Fizz");
} else if (divisibleBy5) {
// Divides by 5, add Buzz
ans.add("Buzz");
} else {
// Not divisible by 3 or 5, add the number
ans.add(Integer.toString(num));
}
} return ans;
} public java.util.List<String> fizzBuzz2(int n) {
// ans list
java.util.List<String> ans = new java.util.ArrayList<String>(); for (int num = 1; num <= n; num++) { boolean divisibleBy3 = (num % 3 == 0);
boolean divisibleBy5 = (num % 5 == 0); String numAnsStr = ""; if (divisibleBy3) {
// Divides by 3, add Fizz
numAnsStr += "Fizz";
} if (divisibleBy5) {
// Divides by 5, add Buzz
numAnsStr += "Buzz";
} if (numAnsStr.equals("")) {
// Not divisible by 3 or 5, add the number
numAnsStr += Integer.toString(num);
} // Append the current answer str to the ans list
ans.add(numAnsStr);
} return ans;
} public java.util.List<String> fizzBuzz3(int n) { // ans list
java.util.List<String> ans = new java.util.ArrayList<String>(); // Hash map to store all fizzbuzz mappings.
java.util.HashMap<Integer, String> fizzBizzDict = new java.util.HashMap<Integer, String>() {
{
put(3, "Fizz");
put(5, "Buzz");
}
}; for (int num = 1; num <= n; num++) { String numAnsStr = ""; for (Integer key : fizzBizzDict.keySet()) { // If the num is divisible by key,
// then add the corresponding string mapping to current
// numAnsStr
if (num % key == 0) {
numAnsStr += fizzBizzDict.get(key);
}
} if (numAnsStr.equals("")) {
// Not divisible by 3 or 5, add the number
numAnsStr += Integer.toString(num);
} // Append the current answer str to the ans list
ans.add(numAnsStr);
} return ans;
} @org.junit.Test
public void test() {
System.out.println(fizzBuzz1(15));
System.out.println(fizzBuzz2(15));
System.out.println(fizzBuzz3(15));
}
}

LeetCode_412. Fizz Buzz的更多相关文章

  1. 【leetcode】412. Fizz Buzz

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

  2. [LeetCode] Fizz Buzz 嘶嘶嗡嗡

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

  3. Lintcode 9.Fizz Buzz 问题

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

  4. LeetCode 412. Fizz Buzz

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

  5. LeetCode Fizz Buzz

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

  6. Fizz Buzz

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

  7. LintCode (9)Fizz Buzz

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

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

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

  9. Swift完成fizz buzz test

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

随机推荐

  1. jmeter源码环境(IDEA)

    jmeter源码环境(IDEA) jmeter 1. 本地环境 2. 下载源码 3. 下载依赖包 4. 导入IDEA 5. 运行 1. 本地环境 Windows7 java版本:1.8.0_191 a ...

  2. django-发送文件

    客户端授权密码”,勾选“开启”,弹出新窗口填写手机验证码. settings.py配置 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBac ...

  3. Scanner的常用用法

    通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给Scanner. s.useDelimiter(" |,|\ ...

  4. yii2 oracle 原生sql分页

    $sql_list = "SELECT ID, FID, INSID, FLIGHTNO, DEPNAME, ARRNAME, to_char(DEPDATE,'yyyy-MM-dd HH2 ...

  5. Tips on Python

    python是一种解释性文件,代码要通过解释器解释运行.python解释器就是python.exe这个程序. pip也是一个pip.exe的程序,是用来管理python的第三方库. 有两种执行方式:脚 ...

  6. Goexit

    package main import ( "fmt" "runtime" ) func test() { defer fmt.Println("cc ...

  7. 使用docker 实现MySQL主从同步/读写分离

    1. 利用 docker 实现 mysql 主从同步 / 读写分离 为了保证数据的完整和安全,mysql 设计了主从同步,一个挂掉还可以用另个.最近重构论坛,想来改成主从吧.担心失误,就先拿 dock ...

  8. JavaScript基础06——字符串

    字符串的创建: 字符串的创建: var str = "hello world"; //常量,基本类型创建 var str2 = new String("hello wor ...

  9. hibernate工具类

    因为hibernate的代码大部分都是固定的,为了将减少重复的代码的书写,可以将这些代码封装为一个工具类,获取hibernate的session对象. 1.工具类: package pers.zhb. ...

  10. THUPC&CTS 2019 游记

    day ? 去THU报了个到. day? THUPC比赛日,三个人都没有智商,各种签到题不会做,被各路神仙吊着打.G题还猜了个假结论,做了好久都不对.最后顺利打铁了. 还顺便去看一下THUAC. da ...