LeetCode 412. Fizz Buzz
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的更多相关文章
- 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(easy)
题目: Write a program that outputs the string representation of numbers from 1 to n. But for multiples ...
- 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 ...
随机推荐
- pycharm svn服务器访问不了的问题。
昨天使用pycharm访问svn服务器的时候一直登陆不上. 总结一下遇到的svn的问题. 1.用户名密码错误.简单致命, 2.svn访问权限,这个是svn服务器那边可以设置的. 3.svn路径拼写错误 ...
- 玩QQ游戏,见到好几个图像是美女的QQ,就不始玩
玩QQ游戏,见到好几个图像是美女的QQ,光占坑就是不开始玩 加了一个,发现是传播不良网站的QQ 聊天还是自动的 估计是利用webqq写的程序,也就那几句话来回重复,让你去注册网站什么 可以加这个Q去体 ...
- 【Android自学日记】使用DatePicker以及TimePicker显示当前日期和时间
DatePicker 1.获取一个日历对象: Calendar cal=Calendar.getInstance(); 2.获取当前日期及时间: int year=cal.get(Calendar.Y ...
- 整理:Javascript获取数组中的最大值和最小值的方法汇总
方法一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 //最小值 Array.prototype.min = function ...
- 【Python文件处理】递归批处理文件夹子目录内所有txt数据
因为有个需求,需要处理文件夹内所有txt文件,将txt里面的数据筛选,重新存储. 虽然手工可以做,但想到了python一直主张的是自动化测试,就想试着写一个自动化处理数据的程序. 一.分析数据格式 需 ...
- java中 IndexOf()、lastIndexOf()、substring()的用法
public int indexof(String str)返回字符串中出现str的第一个位置 public int indexof(String str,int fromIndex)返回字符串中从f ...
- JSON Accelerator真是个好东西...
支持OBJC,JAVA,Python,OBJC(Core Data),Python(Django) 并实现了NSCoding和NSCoping,方便归档和复制. 再也不用辛辛苦苦的写Model了.. ...
- linux Mint 安装tomcat8
先安装jdk,由于我这以安装jdk这里就不做详细描述: 到官网下载和自己jdk对应版本的tomcat包(tomcat.apache.org) 解压tomcat包到/opt/tomcat8下 tar - ...
- 文件权限:普通(r、w、x)和特殊(s、t)
linux中,常常涉及到权限的问题.文件的权限有3组,owner,group,other,每一组都有rwx三种设置.r(4):可读:w(2):可写:x(1):可执行.我们在执行shell命令ls - ...
- Home 安转beta版软件
今天想装测试版的cocoapods,用 brew install cocoapods 后,总是安装稳定版,就是1.1.0,不是最新的beta版,发现用下面这个命令可以装最新beta版 brew ins ...