LeetCode - 412. Fizz Buzz - ( C++ ) - 解题报告 - to_string
1.题目大意
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”.
给定一个数n,要求写出代表1到n的字符串,其中能被3整除的输出 “Fizz”,能被5整除的输出 “Buzz”,同时被3和5整除的输出 “FizzBuzz”。
2.思路
思路非常简单。是个人应该都能做出来。但有几点可以学习一下,一个是C++中vector的应用(可以参考这篇文章),另一个是C++中int转string的方法。
下面是int转string的几种思路:
(1)利用stringstream:
使用stringstream的时候要注意加#include"sstream"。比如说我要把int类型的23转为string类型,那么我可以这样实现:
int a=23;
stringstream ss;
ss<<a;
string s1 = ss.str();
(2)利用sprintf int->char[]
(3)利用itoa int->char[]
(4)利用to_string (详见本题AC代码)
3.代码
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> s;
for(int i=1;i<=n;i++)
{
if(i%15==0)
s.push_back("FizzBuzz");
else if(i%3==0)
s.push_back("Fizz");
else if(i%5==0)
s.push_back("Buzz");
else
s.push_back(to_string(i));
}
return s;
}
};
LeetCode - 412. Fizz Buzz - ( C++ ) - 解题报告 - to_string的更多相关文章
- 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(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
problem 412. Fizz Buzz solution: class Solution { public: vector<string> fizzBuzz(int n) { vec ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- Knowledge Point 20180305 十进制转换成二进制浮点数
如何将十进制的浮点数 转换二进制的浮点数,分为两部分: 1. 先将整数部分转换为二进制, 2. 将小数部分转换为二进制, 然后将整数部分与小数部分相加. 以 20.5 转换为例,20转换后变为1010 ...
- sql1999语法
1.交叉连接 cross join 左右两个表进行组合,产生笛卡尔积累. 左边每一行分别于右表每一行数据匹配. 2.using using使用的前提是两个表右关联的字段需要对应,两个表的join查询. ...
- HDU 1411--校庆神秘建筑(欧拉四面体体积计算)
校庆神秘建筑 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- vue 整体引入 mint-ui 样式失败
当引入Mint-ui 整体css 时 如果出现了这样的错误, 是指找不到对应的Mint-UI 的css :需要从node_modules里寻找 解决方法是在webpack.config.js(有的项目 ...
- tp3.2源码解析——入口文件
如果有人读这篇文章并跟着做的话,希望你能使用支持函数跳转的编辑器,还要善用var_dump和exit,对着源码去调试着看.跟着入口文件读,执行到哪里你看到哪里,对于那些不能一眼看出来的配置,则要记录下 ...
- hadoop 1.x 集群环境的搭建
本文主要以个人工作学习总结为主,同时也为了方便更多的兴趣爱好者参与学习交流,现将具体的搭建步骤分享如下: 一.基础环境 1.1 jdk的安装与配置 Hadoop是用Java开发的,Hadoop的编译及 ...
- Leecode刷题之旅-C语言/python-217存在重复元素
/* * @lc app=leetcode.cn id=217 lang=c * * [217] 存在重复元素 * * https://leetcode-cn.com/problems/contain ...
- C Mingw gcc printf 刷新缓冲行
C Mingw gcc printf 刷新缓冲行 参考:https://stackoverflow.com/questions/13035075/printf-not-printing-on-cons ...
- Random类与Random方法
class Test{ public static void main(String[] args){ int min=2; //定义随机数的最小值 int max=102; //定义随机数的最大值 ...
- Verilog_Day3
内容为书中第5章 条件语句 条件语句必须在过程块语句中使用.所谓过程块语句是指由 initial 和 always 语句引导的执行语句集合.除这两种块语句引导的begin_end块中可以编写条件语句外 ...