题目:

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)的更多相关文章

  1. Java实现 LeetCode 412 Fizz Buzz

    412. Fizz Buzz 写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz" ...

  2. [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

  3. LeetCode 412. Fizz Buzz

    Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...

  4. LeetCode 412 Fizz Buzz 解题报告

    题目要求 Write a program that outputs the string representation of numbers from 1 to n. But for multiple ...

  5. 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 ...

  6. 【leetcode】412. Fizz Buzz

    problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...

  7. 【LeetCode】412. Fizz Buzz 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 方法一:遍历判断 方法二:字符串相加 方法三:字典 日期 [L ...

  8. 力扣(LeetCode)412. Fizz Buzz

    写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...

  9. [LeetCode&Python] Problem 412. Fizz Buzz

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

随机推荐

  1. Android Studio 那些事|Activity文件前标识图标显示为 j 而是 c

    问题:Activity文件前标识图标显示为 j 而是 c 的图标,或是没有显示,并且自己主动提示不提示 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/fo ...

  2. iOS 移动开发周报

    iOS 移动开发周报   前言 是的,我又开始写周报了!主要是因为喵神不写周报了,加上我发现大家对写 iOS 技术周报这件事情似乎没什么兴趣.其实我觉得这是一个挺好的学习总结的办法,所以要不就继续我来 ...

  3. 关于erlang中的timer:tc/3

    timer:tc/3对于统计函数运行时间是个很不错的函数, 截图timer:tc/1,tc/2,tc/3的API: 拿斐波那契数列入手做个讲解: -module(fib). -export([fib/ ...

  4. 【重磅干货】看了此文,Oracle SQL优化文章不必再看!

    目录 SQL优化的本质 SQL优化Road Map 2.1 制定SQL优化目标 2.2 检查执行计划 2.3 检查统计信息 2.4 检查高效访问结构 2.5 检查影响优化器的参数 2.6 SQL语句编 ...

  5. 2015最新iherb海淘攻略-图文入门教程-6月免邮

    注:仅仅有首次下单才享有新人优惠10$,大家下单之后千万不要取消后.否则之后则不享有新人优惠. 注:眼下Sino-海淘客国际物流已取消,仅有UCS合众速递. IHerb是美国最热门的海淘海购网站之中的 ...

  6. UVA - 1045 The Great Wall Game(二分图最佳完美匹配)

    题目大意:给出棋盘上的N个点的位置.如今问将这些点排成一行或者一列.或者对角线的最小移动步数(每一个点都仅仅能上下左右移动.一次移动一个) 解题思路:暴力+二分图最佳完美匹配 #include < ...

  7. Create an OData v4 Endpoint Using ASP.NET Web API 2.2(使用ASP.NET Web API 2.2创建OData v4端点)

    开放数据协议Open Data Protocol(OData)是web的一种数据存取协议,OData通过设置CRUD操作(Create创建.Read读取.Update更新,Delete删除)提供一种统 ...

  8. 导入EXCEL 时间数据为小数 问题

    同事在做将EXCEL导入数据库功能时发现一个奇怪的问题:在EXCEL中,有一列数据明明呈现出时间格式,比如:18:35,但导到数据库中,居然一串长长的小数:0.7743055555555556,我靠, ...

  9. c# 委托 Predicate的使用示例

    一.说明 委托Predicate 可以有参数(比如下面的示例),也可以不带参数,委托Predicate是返回固定值bool值的委托 二.示例代码(控制台程序) using System; using ...

  10. python错误提示“TabError: inconsistent use of tabs and spaces in indentation”

    在遍历打印10以内的奇数是出现“TabError: inconsistent use of tabs and spaces in indentation”的错误提示: 代码如下: 第一感觉没什么错误, ...