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/ 题目地 ...
随机推荐
- Python 学习笔记(九)Python元组和字典(三)
字典常用方法 copy() 返回一个字典的浅复制 示例:浅拷贝d.copy() 深拷贝引入import copy copy.deepcopy() >>> help(dict.co ...
- easyui 上 datagrid 的表头的checkbox全选时 取消选中 disabled的checkbox
业务需求: 正常情况下,easyui的全选checkbox会选择表中全部的checkbox包括行.及时对checkbox加了disable属性也没有效果.但是现在的业务是当对checkbox加了dis ...
- 2018 CVTE 前端校招笔试题整理
昨天晚上(7.20)做了CVTE的前端笔试,总共三十道题,28道多选题,2道编程题 .做完了之后觉得自己基础还是不够扎实,故在此整理出答案,让自己能从中得到收获,同时给日后的同学一些参考. 首先说一下 ...
- hdu Hat's Fibonacci(用了kuangbin模板)
大数的位数设置很坑,设成700会越界,设成800会超空间,最后设成了750居然就过了.... #include <iostream> #include <cstdio> #in ...
- <CPP学习 第二天> 字符串的输入 及 String类
今天简单的学习了字符串的输入以及C++的String类. 1.面向行的输入: getline(); getline()函数读取整行,通过回车键输入的换行符来确定输入结尾.要调用这种方法,可以使用cin ...
- ORA-00911: 无效字符 问题和解决
1.原本java插入数据库表数据写法是这样的 String sql = "INSERT INTO AAA (id1,id2,id3,id4) VALUES ('1','2','3','4') ...
- Python入门 —— 06语音识别
Python 语音 实现语音操控的原理 语音操控分为语音识别和语音朗读两部分 我们使用speech模块实现语音模块(python 2.7) SAPI是微软Speech API , 是微软公司推出的语音 ...
- HTML表格-table
表格 表格由 <table> 标签来定义. 每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由 <td> 标签定义). 字母 td 指表格数据 ...
- Thinkphp5 对接百度云对象存储 BOS (上传、删除)
首先下载SDK包可以在 官网下载,或者在项目根目录使用composer . composer require baidubce/bce-sdk-php 压缩包里面有五个文件,实际运用到只有两个,然后放 ...
- Spring quantz--定时任务调度工具
1.在xml中交给spring管理的一些类 <bean id="cancelOrderJobDetail" class="org.springframework.s ...