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

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

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

  4. LeetCode 412 Fizz Buzz 解题报告

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

  5. 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 ...

  6. 【leetcode】412. Fizz Buzz

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

  7. 【LeetCode】412. Fizz Buzz 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 方法一:遍历判断 方法二:字符串相加 方法三:字典 日期 [L ...

  8. 力扣(LeetCode)412. Fizz Buzz

    写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...

  9. [LeetCode&Python] Problem 412. Fizz Buzz

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

随机推荐

  1. Code Snippets 代码片段

    Code Snippets 代码片段       1.Title : 代码片段的标题 2.Summary : 代码片段的描述文字 3.Platform : 可以使用代码片段的平台,有IOS/OS X/ ...

  2. CSS基础总结

    CSS基础总结链接地址:http://segmentfault.com/a/1190000002773955

  3. 改变win7驱动图标

    一.背景 自己做的USB设备是HID设备,注册到设备管理器中就是"HID Compliant device",显得很业余,然后想去改变这个图标和名称,也就有了此篇文章 二.正文 还 ...

  4. js 中histroy.back()与history.go()的区别

    样例: js6.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...

  5. 由表单验证说起,关于在C#中尝试链式编程的实践

    在web开发中必不可少的会遇到表单验证的问题,为避免数据在写入到数据库时出现异常,一般比较安全的做法是前端会先做一次验证,通过后把数据提交到后端再验证一次,因为仅仅靠前端验证是不安全的,有太多的htt ...

  6. JavaScript对象创建,继承

    创建对象 在JS中创建对象有很多方式,第一种: var obj = new Object(); 第二种方式: var obj1 = {};//对象直面量 第三种方式:工厂模式 function Per ...

  7. em与px换算关系以及常用列表

    1.任意浏览器的默认字体大小都是16px.2.所有未经调整的浏览器都符合: 1em=16px 12px=0.75em 10px=0.625em3.为了简化font-size的换算,在body选择器中声 ...

  8. 用open_gapps安装google play

    说明  一个开放源码脚本自动生成最新的谷歌应用程序包.对整个google play 程序框架的打包,包括一些google官方的程序.对于阉割了google ply用户来说是一个不错的选择. 使用 下载 ...

  9. nginx配置反向代理解决前后端分离跨域问题

    摘自<AngularJS深度剖析与最佳实践>P132 nginx配置文件如下: server { listen ; server_name your.domain.name; locati ...

  10. js日期相关

    时间戳转正常日期时间 1469512964000 —> 2016/7/26 下午2:02 var getLocalTime = function(nS) { // 13位时间戳 return n ...