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 ...
随机推荐
- HDU-4544 湫湫系列故事——消灭兔子 (贪心+优先队列)
题目思路 将兔子的血量从大到小排列,将箭的属性写在类中(结构体也成),排序按照伤害从大到小排列,若有相等的则按价格从小到大排. 代码 #include<bits/stdc++.h> usi ...
- 从url中下载资源(目前测试只有照片,文件类的没有进行测试)
首先:是工具类: public class DownLoadUtils { /** * 从网络Url中下载文件 * * @param urlStr url路径 * @param fileName 文件 ...
- 数据库连接的配置文件activation节点
在数据库连接连接不上的时候,一定要查看一下数据库的配置文件,<activeByDefault>这个节点比较关键,表示的是默认的连接数据库节点,当然配置文件中只能有一个这样的节点.否则是会报 ...
- Java 内部类和Lambda
Java内部类 内部类又称为嵌套类,是在类中定义另外一个类.内部类可以处于方法内/外,内部类的成员变量/方法名可以和外部类的相同.内部类编译后会成为完全不同的两个类,分别为outer.class和ou ...
- api的url规则设计,带参数的路由
api的url设计规则 router := gin.Default() router.GET("/topic/:topic_id", func(context *gin.Conte ...
- Spark-源码分析01-Luanch Driver
1.SparkSubmit.scala 什么是Driver 呢?其实application运行的进程 就是driver,也是我们所写的代码就是Driver. object DefaultPartiti ...
- learning java transient 自定义序例化
public class Person implements java.io.Serializable { private String name; private transient int age ...
- [RN] React Native 使用 react-navigation 报错 "Unable to resolve module `react-native-gesture-handler`
在React Native 使用 react-navigation 过程中,报错 "Unable to resolve module `react-native-gesture-handle ...
- string拼接时去掉最后一个逗号
str.replace(str.length() - 1, str.length(), "");
- CSP2019自闭记
为什么我之前没有写呢,是因为我总是考的太lj,于是就不想写了. 这次不管考没考好都要强迫自己写,因为这是第一次参加提高组+第一次参加CSP. 当然什么初赛/复赛试题/答案什么的是不会出现的. Day ...