题目:

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. 在Fedora 25中更换openjdk为oracle jdk

    本文修改自csdn: openjdk的好处是: 1.升级方便,fedora团队社区负责维护升级,安全稳定,质量有保证. 2.已经支持了很多应用:而且还越来越强大 3.支持eclipse开发. 实际上, ...

  2. 生产追溯系统-Wifi+传感器,实现计数器以及监控机器是否停止

    物联网听上去是一个高大上的词儿,还有什么大数据.云.智能制造等等,今天我也往这方面稍微靠一靠,这篇文章主要介绍的是通过 wifi 模块与传感器组合,实现感应计数器,应用场景主要如下: 1.统计 SMT ...

  3. jsp 下拉框首字母定位可检索

    实现效果如图: 页面部分: (1)js中: (2)body中: JAVA代码部分: 控制器Controller中 写一个页面js中调用的方法: 引入jar包:

  4. PythonCookBook笔记——数字日期和时间

    数字日期和时间 数字的四舍五入 用round函数,指定值和小数位数. >>> round(1.23, 1) 1.2 >>> round(1.27, 1) 1.3 & ...

  5. Java中ASM框架详解

    什么是asm呢?asm是assembly的缩写,是汇编的称号,对于java而言,asm就是字节码级别的编程.  而这里说到的asm是指objectweb asm,一种.class的代码生成器的开源项目 ...

  6. JS中小数如何转化为百分数并能四舍五入

    <script type="text/javascript">//n表示百分数保留的位数 function toPercent(n){ n = n || 2; retu ...

  7. Unity3d 新建xml 读取xml

    在游戏开发中.Xml常常被用来作为技能配置.地图配置.人物动作配置等配置文件. Unity3d内置的Xml库让我们非常方便地就能够新建Xml和读取Xml. 以下是一个样例,新建了一个Xml文档.而且读 ...

  8. 九度OJ 1120:全排列 (DFS)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4471 解决:1139 题目描述: 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' < ...

  9. BitMapFactory.decodeFile 总是返回NULL的原因之一

    之前碰到过,顺便记录下来,如果出现这种情况,首先看看是否有对这个文件的读权限.

  10. 使用 Visual Studio Code 运行 C# 及 Java 程序

    背景 很多情况下,我只是想要编写一个非常简单的 C# 或者 Java 程序,只有几行代码,看看运行结果而已.虽说 Visual Studio / Eclipse / IntelliJ IDEA 功能强 ...