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. oracle 12如何解锁账户锁定状态及修改忘记的密码

    有两种方法,大同小异吧,感觉命令真是个好东西,哈哈哈哈,挽救了我安了4次才安好的oracle!!! 方法一: 1.如果忘记密码,找到忘记密码的是哪个用户身份,如果用户被锁定,可以使用下面说的方法解除锁 ...

  2. python多线程知识-实用实例

    python多线程使用场景:IO操作,不适合CPU密集操作型任务   1.多个线程内存共享 2.线程同时修改同一份数据需要加锁,mutex互斥锁 3.递归锁:多把锁,锁中有锁 4.python多线程, ...

  3. android软件开发之TextView控件常用属性

    TextView控件 text属性,设置显示的文本 textColor:设置文本颜色 textSize:设置文本字体大小 autoLink:设置文本为电话,URL连接等的时候是否显示为可点击的链接 c ...

  4. 微信小程序中的app文件介绍

    [app] 一.app.json 1.对当前小程序的全局配置 2.页面路径.界面表现.网络超时时间.底部 tab 等 { "pages":[ "pages/index/i ...

  5. 简单的反编译class文件并重新编译的方法

    在没有.java源码的情况下,如果想修改一个.class文件.可以通过以下步骤实现: 修改前的class文件: 一.反编译.class文件成.java文件. 1.可以使用Java Decompiler ...

  6. Java 序列化与反序列化(Serialization)

    一.什么是?为什么需要? 序列化(Serialization)是将对象的状态信息转化为可以存储或者传输的形式的过程,反序列化则为其逆过程. 内存的易失性:传输需要:一些应用场景中需要将对象持久化下来, ...

  7. Django学习笔记1

    重点在注释# 1.views.py from django.shortcuts import render from django.http import * #from django.templat ...

  8. ;(function($,window,document,undefined){})(jQuery,window,document)

    ;(function($,window,document,undefined){})(jQuery,window,doucment) 1.自调函数(function(){})() 2.好处是不会产生任 ...

  9. DataSet和泛型之间相互转换

    取数据的时候,存储过程返回了多个结果集,后台用DataSet去接收这几个结果集,然后接收之后,需要将结果集转换为不同的实体,于是下面的代码便出现了. /// <summary> /// 将 ...

  10. Delphi7 GDI+学习

    Delphi7自带的绘图有锯齿,所以要学习GDI+ 主要是从这个网站学习 http://www.bianceng.com/Programming/Delphi/201212/34691.htm 相关控 ...