LeetCode_412. Fizz Buzz
412. Fizz Buzz
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的更多相关文章
- 【leetcode】412. Fizz Buzz
problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...
- [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 ...
随机推荐
- MSSQL-反弹注入
工具:香港云免费云服务器:http://www.webweb.com 注册使用匿名邮箱:https://bccto.me/ 香港云服务器搭建MSSQL数据库,并建表admin,字段数要大于等于我们想要 ...
- 离线安装docker(RedHat7.4)
离线安装docker(RedHat7.4) docker 1. 下载地址 2. 解压并注册为service 1. 下载地址 官网下载地址:下载 官网文档地址:文档 2. 解压并注册为service 下 ...
- 关于c++中的类型转换符
const_cast(链接) 用来去掉const或volatile属性 volatile: 用于并行设备的硬件寄存器(状态寄存器), 中断服务子程序中会访问到的非自动变量, 多线程中被几个任务共享的变 ...
- MySQL命令操作(Linux平台)
Linux shell 批量创建数据库/表 Shell 脚本如下: # create database and table HOST='localhost' PORT='3306' USER='roo ...
- Apache Solr < 8.2.0远程命令执行漏洞(CVE-2019-0193)
介绍:Apache Solr 是一个开源的搜索服务器.Solr 使用 Java 语言开发,主要基于 HTTP 和 Apache Lucene 实现. 漏洞原因:此次漏洞出现在Apache Solr的D ...
- CSP2019 J组 游记
结果 分数出来了.100+100+10+35=245. 一等线230,擦着边进一等. (点击图片放大) 期待明年s组的表现. 第一轮 不就是初赛吗?擦边轻松水过去! 第二轮 Day -14 停两周晚自 ...
- rhcsa备战笔记
笔记全部手打 转载请加原文链接 0)重置密码开机按e 找到linux16行 rd.break console=tty0 ctrl+xmount -o remount,rw /sysrootchroo ...
- LOJ P10002 喷水装置 题解
每日一题 day35 打卡 Analysis 先将不符合条件的区间去掉(即半径小于W,不然宽度无法符合),将符合条件的按区间存入节点中.区间的左边界是x-sqrt(r*r-W*W/4.0),要计算x轴 ...
- 【洛谷】P1449 后缀表达式
P1449 后缀表达式 分析: 简单的模拟题. 熟练容器stack的话很容易解决. stack,栈,有先进后出的特性. 比如你有一个箱子,你每放进第一个数时,就往箱底放,放第二个数时就在第一个数的上面 ...
- 深度学习图像配准 Image Registration: From SIFT to Deep Learning
Image Registration is a fundamental step in Computer Vision. In this article, we present OpenCV feat ...