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. 你真的理解CountDownLatch与CyclicBarrier使用场景吗?

    原文:https://blog.csdn.net/zzg1229059735/article/details/61191679 相信每个想深入了解多线程开发的Java开发者都会遇到CountDownL ...

  2. 动态生成16位不重复随机数、随机创建2位ID

    /** 1. * 动态生成16位不重复随机数 * * @return */ public synchronized static String generate16() { StringBuffer ...

  3. Centos7服务器搭建部署显卡计算环境以及常用软件的安装使用

    安装好anaconda的服务器上会more你已经安装好jupyter notebook,执行下面的命令可以提供链接地址允许远程浏览器打开并访问: jupyter notebook --no-brows ...

  4. evpp tcp server服务端

    // netserver.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <evpp/tcp_server.h> ...

  5. 项目(一)--python3--爬虫实战

    最近看了python3网络爬虫开发实战一书,内容全面,但不够深入:是入门的好书. 作者的gitbook电子版(缺少最后几章) python3网络爬虫实战完整版PDF(如百度网盘链接被屏蔽请联系我更新) ...

  6. VMware共享本地文件

    最后可以在这里找到

  7. Java中的map的遍历方法

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

  8. JVM JDK1.8 以后的新特性 VisualVM的安装使用

    一.JVM在新版本的改进更新以及相关知识 1.JVM在新版本的改进更新 图中可以看到运行时常量池是放在方法区的 1.1对比: JDK 1.6 及以往的 JDK 版本中,Java 类信息.常量池.静态变 ...

  9. c#——ref 和 out 的区别

    一个用关键字 ref 标示,一个用 out 标示. 牵扯到数据是引用类型还是值类型. 一般用这两个关键字你是想调用一个函数将某个值类型的数据通过一个函数后进行更改.传 out 定义的参数进去的时候这个 ...

  10. SpringMVC_原理(转)

    在整个Spring MVC框架中,DispatcherServlet处于核心位置,它负责协调和组织不同组件完成请求处理并返回响应的工作.具体流程为:1)客户端发送http请求,web应用服务器接收到这 ...