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. C#锁对象代码

    private static readonly object SequenceLock = new object(); private static readonly object SequenceL ...

  2. 玩转DNS服务器——Bind服务

    合理的配置DNS的查询方式 实验环境: 虚拟机:VMware® Workstation 15 Pro 均使用NAT连接   网段为192.168.1.0/24 DNS 服务器 ---- Centos ...

  3. docker postgresql 数据库

    1. 使用docker 镜像 获取镜像:docker pull postgres:9.4 启动: docker run --name postgres1 -e POSTGRES_PASSWORD=pa ...

  4. SQL中的trim函数

    Oracle TRIM函数是很常见的函数,下面对Oracle TRIM函数的语法作了详尽的阐述说明,希望可以让您对Oracle TRIM函数有更深的认识. 如果提到Oracle TRIM函数,最简单的 ...

  5. Yarn (转自之乎者也)

    作者:青俞链接:https://www.zhihu.com/question/34016617/answer/57822812来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  6. python变量选中后高亮显示

    file>settings>editor>color scheme>general>code>identifier under caret>backgroun ...

  7. linux 读取文件

    linux读取文件是经常要用到的操作,以下示例(说明看注释): #读取文件snlist.txt中的每一行内容赋给sn变量 while read sn do echo ">>> ...

  8. Django API view 登录认证

    文件分类 url from django.contrib import admin from django.urls import path, re_path from django.urls imp ...

  9. OLED液晶屏幕(1)OLED液晶屏幕ssd1306驱动芯片 arduino运行 ESP8266-07可以 12f不可以

    OLED屏幕有各种形状和尺寸,但目前有两种非常受欢迎的屏幕尺寸. 1)0.96“ 2)1.3“ 他们也有2种常见的颜色 1)蓝色 2)白色 驱动OLED的芯片常用的有两种.这两种芯片有许多非常相似的设 ...

  10. 用 ThreadPoolExecutor/ThreadPoolTaskExecutor 线程池技术提高系统吞吐量(附带线程池参数详解和使用注意事项)

    1.概述 在Java中,我们一般通过集成Thread类和实现Runnnable接口,调用线程的start()方法实现线程的启动.但如果并发的数量很多,而且每个线程都是执行很短的时间便结束了,那样频繁的 ...