LeetCode Fizz Buzz
原题链接在这里:https://leetcode.com/problems/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"
]
题解:
从1到n, 当前数能否被15, 5, 3整除,添加对应String. 均不能整除添加当前数.
Time Complexity: O(n). Space: O(1) regardless res.
AC Java:
public class Solution {
public List<String> fizzBuzz(int n) {
List<String> res = new ArrayList<String>();
for(int i = 1; i<=n; i++){
if(i%5 == 0 && i%3 == 0){
res.add("FizzBuzz");
}else if(i%5 == 0){
res.add("Buzz");
}else if(i%3 == 0){
res.add("Fizz");
}else{
res.add(String.valueOf(i));
}
}
return res;
}
}
LeetCode Fizz Buzz的更多相关文章
- LeetCode——Fizz Buzz
LeetCode--Fizz Buzz Question Write a program that outputs the string representation of numbers from ...
- [LeetCode] Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- LeetCode算法题-Fizz Buzz(Java实现)
这是悦乐书的第221次更新,第233篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第88题(顺位题号是412). 编写一个程序,输出从1到n的数字的字符串表示.但对于三的 ...
- [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- 【LeetCode】412. Fizz Buzz 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 方法一:遍历判断 方法二:字符串相加 方法三:字典 日期 [L ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- 【leetcode】412. Fizz Buzz
problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...
- Leetcode 414.Fizz Buzz By Python
写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...
- 力扣(LeetCode)412. Fizz Buzz
写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...
随机推荐
- linux xorddos样本分析2
逆向分析 之后我们通过ida对该样本进行更深入的分析样本的main函数中,一开始会调用函数dec_conf对样本中的大量加密的字符串进行解密,如下图所示.
- linux学习笔记-前篇
大学毕业已经快三年了,从事嵌入式开发的工作也快三年了. 不过,老干些裸机开发,感觉很是枯燥,一咬牙一跺脚,决定从今天开始学习Linux操作系统,顺便记录下学习过程中所遇到的问题与心得. 自己从前完全没 ...
- CodeForces 515B. Drazil and His Happy Friends
B. Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes in ...
- 【原】iOS学习之Socket
Socket在百度百科的定义 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 相关的描述 Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进 ...
- Codeforces Round #254 (Div. 2) A DZY Loves Chessboard
先生成nXm的BW棋盘 BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB 类似上面交替变换 然后将输入为’-’的地方替换成‘-’即可 #include <iostream& ...
- 不想说作用域scope,因为是scopeTree,
ps:本文前面大部分是错的,后边大部分也是错的,搞混了不要怪我................... 这篇文章讲述了一个悲伤的故事,从一个似似而非的概念一步一步到错误的理解,最后勉强正确的过程 其实我 ...
- CSS will-change 属性
介绍 如果你注意到在webkit的浏览器上“flicker”一些CSS操作(尤其是变形和动画方面的)的表现,你很可能之前就注意过硬件加速了 CPU.GPU和硬件加速 硬件加速意味着Graphics P ...
- C语言_第一章
1. 计算机能直接识别和接受的二进制代码称为 机器指令——>(集合) 机器语言. 2. 输出C #include<stdio.h> int main(){ printf(&q ...
- Android -- 自动完成文本框(可以匹配多个值,并以,结尾)
1.
- Java web servlet 拦截器 以登陆为例子
以登陆为例子............... public class LoginFilter implements Filter { @Override public void destroy() { ...