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 ...
随机推荐
- C# 之 集合ArrayList
.NET Framework提供了用于数据存储和检索的专用类,这些类统称集合. 这些类提供对堆栈.队列.列表和哈希表的支持.大多数集合类实现系统的接口.以下我们主要来讲一下ArrayList. ...
- EasyRTMP实现将RTSP流转换成RTMP流实现RTSP直播转RTMP直播的功能
本文转自EasyDarwin开源团队Kim的博客:http://blog.csdn.net/jinlong0603/article/details/52951311 EasyRTMP EasyRTMP ...
- 九度OJ 1101:计算表达式 (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4340 解决:1335 题目描述: 对于一个不存在括号的表达式进行计算 输入: 存在多种数据,每组数据一行,表达式不存在空格 输出: 输出结 ...
- 解决Pods Unable to find a specification for `xxxxx`问题
错误信息为 Unable to find a specification for *RMQClient* (~> 1.x.x) depended upon by Podfile 刚开始以为这个已 ...
- 当半导体的工艺制程走到7nm后
https://mp.weixin.qq.com/s/LjFTtEKFX2o8kLjn3y6GbQ 深度学习的异构加速技术1:效率因通用而怠,构架为AI而生 一方面,当半导体的工艺制程走到7nm后,已 ...
- Impala 安装笔记2一hive和mysql安装
l 安装hive,hive-metastore hive-server $ sudo yum install hive hive-metastore hive-server l 安装mysql ...
- Raspberry Pi3 ~ 安装 nfs Server
l 安装必要服务: sudo apt-get install portmap sudo apt-get install nfs-kernel-server sudo apt ...
- MysqlNDBcluster集群数据操作可能出现的问题
Ndbcluster 版本7.5: 1.非ndbcluster引擎的表集群不会同步:若要同步,需要使engine=ndbcluster;如果表有外键约束需先删除外键,同步成功后再建立外键[否则会报错] ...
- hadoop 常用命令总结
1. 查看集群资源信息 hdfs dfsadmin -report 2. 启动一个mapreduce任务, hadoop jar /opt/hadoop/share/hadoop/tools/lib/ ...
- source insight 快捷键
source insight 返回上一视图 快捷键:ALT+, 浏览项目符号:F7 全局浏览查找某个名称开头的函数或结构体等