Problem:

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 = , Return:
[
"",
"",
"Fizz",
"",
"Buzz",
"Fizz",
"",
"",
"Fizz",
"Buzz",
"",
"Fizz",
"",
"",
"FizzBuzz"
]

思路较为简单,首先考虑是否为15的倍数,然后考虑3,5的倍数,然后分情况处理即可。

runtime主要受int to string的影响。期初使用stringstream的方法,后换为C++ 11提供的to string。

class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> re;
for(int i = ; i <= n; i++) {
if (i % == )
re.push_back("FizzBuzz");
else if(i % == )
re.push_back("Fizz");
else if(i % == )
re.push_back("Buzz");
else
{
//stringstream ss;
//ss << i;
//re.push_back(ss.str());
re.push_back(to_string(i));
}
}
return re;
}
};

LeetCode 412. Fizz Buzz的更多相关文章

  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(easy)

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

  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. SVN发布网站

    1.问题 2.分析 如果机器先安装.NET framework,后安装IIS就会出现此问题,原因是.NET framework未注册. 3.注册.NET framework

  2. 08-linux 其他知识

    linux其他知识点 1-防火墙- iptables原理 2- 本部分属于一步一步学习大数据系列之 Linux 的章节,欢迎订阅更多文章 更多内容请参考:一步一步学习大数据系列介绍

  3. php时间类

    1.需求 数据库的时间都是用10个长度的时间戳存储,拿出来的时候要转为更易读的格式 2.例子 <?php class Mydate{ public function get_millisecon ...

  4. ContextMenuStrip 类

    表示快捷菜单 命名空间:   System.Windows.Forms程序集:  System.Windows.Forms(位于 System.Windows.Forms.dll) 继承层次结构 Sy ...

  5. centos 6.7 搭建tornado + nginx + supervisor的方法(已经实践)

    首先,本来不想写这篇博客了,但是我测试了很多网上的例子包括简书的,全不行,我总结原因是自己太笨,搞了俩个晚上,后来决定,自己还是写一篇记录下来,保证自己以后使用 环境: centos6.7 64 py ...

  6. iOS 编译时的警告导致无法通过编译

    今天编译react native的代码,发现了2个警告,但是系统却当做错误,不能编译成功,查看了一下编译选项,看到了如下配置: 注意到这个-Werror 了吗? 就是这个标志导致系统把所有的 警告都当 ...

  7. js:使用js过程中遇到的一个小问题

    在一个作业中使用了js,函数A调用函数B.当A和B中均含有变量i的时候,相关操作结果可能会出错. 将B中的i替换为j(j不存在于A中)后,结果正确. 目前考虑原因是两个变量i有相关性(或者说实际上就是 ...

  8. 关于chart.js 设置canvas的宽度为父级元素的宽度的百分百 以及 X轴上面刻度数据太多如何处理

    今天在做一个数据统计的界面的时候,需要做折线统计图,在网上找了一圈发现数据统计的插件还是不少的,本着轻量级的的原则选择了Chart.js,后来在做的过程中便遇到两个问题,以此记录下来,和刚刚接触前端的 ...

  9. SQLServer 2008 R2 对同时含有数字和中文的字段进行排序

    若是数据库中的某一个字段名为bedNO,类型为nvchar,里面有{1,2,3,11,12,23,加2,加3}这些数据. 此时我需要对这些数据进行排序,数字按大小拍前面,汉字按第一个字拼音首字母的顺序 ...

  10. html5 Application Cache 机制以及使用

    那什么是Application Cache呢? 顾名思义,AppCache就是对app内存缓存的方案,具体表现为当请求某个文件时不是从网络获取该文件,而是从本地内存中获取. Application C ...