LeetCode: 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"
]
代码:
自己的:
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> result;
for(int i = ; i < n+; i++){
if ((i% == )&&(i% == ))
result.push_back("FizzBuzz");
else if ((i% != )&&(i% == ))
result.push_back("Buzz");
else if ((i% == )&&(i% != ))
result.push_back("Fizz");
else
result.push_back(to_string(i));
}
return result;
}
};
别人的:
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> res;
for(int i=; i<=n; i++){
string s;
if(i%==) s = "Fizz";
if(i%==) s += "Buzz";
if(s.empty()) s = to_string(i);
res.push_back(s);
}
return res;
}
};
LeetCode: 412 Fizz Buzz(easy)的更多相关文章
- Java实现 LeetCode 412 Fizz Buzz
412. Fizz Buzz 写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz" ...
- [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
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- LeetCode 412 Fizz Buzz 解题报告
题目要求 Write a program that outputs the string representation of numbers from 1 to n. But for multiple ...
- LeetCode - 412. Fizz Buzz - ( C++ ) - 解题报告 - to_string
1.题目大意 Write a program that outputs the string representation of numbers from 1 to n. But for multip ...
- 【leetcode】412. Fizz Buzz
problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...
- 【LeetCode】412. Fizz Buzz 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 方法一:遍历判断 方法二:字符串相加 方法三:字典 日期 [L ...
- 力扣(LeetCode)412. Fizz Buzz
写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...
- [LeetCode&Python] Problem 412. Fizz Buzz
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
随机推荐
- Java UUID 生成(转载)
来自:http://www.cnblogs.com/jdonson/archive/2009/07/22/1528466.html 基本原理:GUID是一个128位长的数字,一般用16进制表示.算法的 ...
- cocos2d-x lua 中使用protobuf并对http进行处理
cocos2d-x lua 中使用protobuf并对http进行处理 本文介绍 cocos2d-x lua 中使用http 和 基于cocos2d-x 对lua http的封装(部分ok) 本博客链 ...
- shift:解决shell编程中的入渗问题
我说过了,shell是我的常规武器,目前虽然还不纯熟,但是我爱shell这门语言,在Linux下面混,总要写脚本.程序员是有基因,对编程语言是有偏好的,你让我写C代码,我会觉得很爽,会有困难,会有痛苦 ...
- maven的基本原理和使用
一.Maven中央存储库 当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载.首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源,如 ...
- 配置tomcat,实现域名访问项目
首先,配置tomcat端口号为80,配置方法:配置tomcat,访问端口改为80 然后,配置访问项目时候,不用项目名,配置方法:配置tomcat,使访问项目时候无项目名 最后,配置tomcat的ser ...
- 《打造Facebook》
王淮的<打造Facebook>一书不厚,花半天时间轻松读完.书中没有大段的说教,只有近乎流水的陈述.正如作者所说,打造Facebook这本书由巴克伯格来写再合适不过.可惜他至少在近几年内没 ...
- 设置VIM查找时不区分大小写
:set ic 永久生效 vi ~/.vimrc set ic
- socket 学习笔记
#include <sys/socket.h> ---------------------------------------------------------------------- ...
- python学习笔记:第四天( 字符串)
Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 在Python2中,普通字符串是以8位ASCII码进行存储的,而Unicode字 ...
- hdu-5754 Life Winner Bo(博弈)
题目链接: Life Winner Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...