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 ...
随机推荐
- Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.qingmu.seller.entity.OrderMaster
org.springframework.orm.jpa.JpaSystemException: ids for this class must be manually assigned before ...
- Beta冲刺(1/7)——2019.5.22
所属课程 软件工程1916|W(福州大学) 作业要求 Beta冲刺(1/7)--2019.5.22 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万里 ...
- 题解 LA2889
题目大意 多组数据,每组数据给出一个正整数 \(n\),输出第 \(n\) 大的回文数(即 \(1,2,3,\cdots\)). 分析 不难发现,\(n\) 位的回文数有 \(9*10^{\lfloo ...
- fastjson<=1.2.47反序列化RCE漏洞
介绍:fastjson是一个Java语言编写的高性能功能完善的JSON库. 漏洞原因:fastjson在解析json的过程中,支持使用autoType来实例化某一个具体的类,并通过json来填充其属性 ...
- go 学习 (一):环境配置
Go 下载地址:https://golang.google.cn/dl/ 右键我的电脑 --> 左上方 “高级系统设置” ---> 环境变量 --> 第二个菜单栏 “系统变 ...
- [Flutter] Flexible the Widget height to available space
Let's say you set widget height to 200, but to different screen, there might not be enough space for ...
- Pivotal Greenplum 6.0 新特性介绍
Pivotal Greenplum 6.0 新特性介绍 在1月12日举办的Greenplum开源有道智数未来技术研讨会上,Pivotal中国研发中心Greenplum 产品经理李阳向大家介绍了Pi ...
- BZOJ 3689: 异或之 可持久化trie+堆
和超级钢琴几乎是同一道题吧... code: #include <bits/stdc++.h> #define N 200006 #define ll long long #define ...
- 2-ESP8266 SDK开发基础入门篇--非RTOS版与RTOS版
https://www.cnblogs.com/yangfengwu/p/11071580.html 所有的源码 https://gitee.com/yang456/Learn8266SDKDevel ...
- [RN] React Native 生成 Android APK
在用模拟器或者真机调试完App后,需要将App打包成Apk发布文件. 下面简单记录下打包步骤: 第一:生成签名密钥 这一步的操作主要是生成需要的签名密钥,供android调用,生成的文件待用 在项目根 ...