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

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

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

  5. LeetCode 412 Fizz Buzz 解题报告

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

  6. 【leetcode】412. Fizz Buzz

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

  7. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. 大数据框架-Hbase

    大规模结构化集群存储数据库.Table中的所有行都按照row key的字典序排列. 主键:row Key.访问行只能通过rowKey访问(范围或者准确值),或者全表扫描: 列族:cloumn fami ...

  2. dubbox provider cosumer 包坐标及xml

    provider pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...

  3. IOS 枚举 enum

    前言:oc中枚举的正确使用,可以增强代码的可读性,减少各种“错误”,让代码更加的规范.下面先介绍枚举的用法,最后介绍个人对枚举的理解,什么是枚举,为什么用枚举. 一. OC中,枚举的使用 1. 写法1 ...

  4. HDU 5572--An Easy Physics Problem(射线和圆的交点)

    An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  5. 实现一个shell程序

    实现一个自己的shell程序,这个程序有这些功能:解释执行命令,支持输入输出重定向,支持管道,后台运行 程序.当运行该程序后,它支持以下的命令格式: 1.单个命令,如:ls.2.带l到多个参数的命令, ...

  6. NPOI读取Excel遇到的坑

    NPOI是POI的.NET版本.POI是用Java写成的库,能帮助用户在没有安装Office环境下读取Office2003-2007文件.NPOI在.NET环境下使用,能读写Excel/Word文件. ...

  7. Linux学习笔记——1.超级用户

    以超级用户工作:su su命令允许临时变换到任何一用户标识(如果拥有口令的话),并挂起当前shell,为新用户开启一个新的shell. su <user> 将当前用户标识harley变换为 ...

  8. Jquery中绑定事件与普通事件的区别

    (“#panel”).bind(“click”,function(){ 与$(“#panel”).click(function(){ 有什么区别 ? 绑定可以同时加多个事件 如:$(“#panel”) ...

  9. [翻译]Hystrix wiki–Home

    注:本文并非是精确的文档翻译,而是根据自己理解的整理,有些内容可能由于理解偏差翻译有误,有些内容由于是显而易见的,并没有翻译,而是略去了.本文更多是学习过程的产出,请尽量参考原官方文档. 什么是Hys ...

  10. sql server 常用sql语句

    --删除约束 alter table productInfo drop constraint 约束名称 --删除列alter table productInfo drop column 列名 --添加 ...