题目描述

写一个程序,输出从 1 到 n 数字的字符串表示。

1. 如果 n 是3的倍数,输出“Fizz”;

2. 如果 n 是5的倍数,输出“Buzz”;

3. 如果 n 同时是3和5的倍数,输出 “FizzBuzz”。

示例:

n = 15,

返回:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]

题目分析

筛素法类似思想, 先初始化所有元素为空字符串, 之后将3的倍数的元素加上”Fizz”, 5的倍数的元素加上”Buzz”, 后遍历整个数组并将数组中空的字符串赋值为次序.

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
大专栏  Leetcode 412.FizzBuzz"line">class  {
public:
vector<string> fizzBuzz(int n) {
vector<string> res;
res.resize(n);
for(int i=1; i<=n/3; ++i) {
res[i*3-1] += "Fizz";
}
for(int i=1; i<=n/5; ++i) {
res[i*5-1] += "Buzz";
}
for(int i=0; i<n; ++i) {
if(res[i] == "") res[i]+=to_string(i+1);
}
return res;
}
};

Leetcode 412.FizzBuzz的更多相关文章

  1. [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡

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

  2. Java实现 LeetCode 412 Fizz Buzz

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

  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 解题报告

    题目要求 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(easy)

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

  7. LeetCode之412. Fizz Buzz

    -------------------------------------------- 虽然是从最简单的开始刷起,但木有想到LeetCode上也有这么水的题目啊... AC代码: public cl ...

  8. 【leetcode】412. Fizz Buzz

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

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

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

随机推荐

  1. SVN解除与服务器的关联

    1.随便找个地方新建个文本文档 2.粘贴如下内容到文档里,建议使用notepad++ Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\ ...

  2. cJSON api的使用教程

    背景说明:由于和后台通信,为了统一数据格式,选择使用json格式,客户端开发语言使用的是c,故需要借助第三方库来实现json格式的转化,cJSON是一个很好的第三方库, 下载链接1:https://g ...

  3. dlib安装踩过的坑

    使用到dlib时遇到了下载错误,不能正常安装 以下是成功安装的记录: 首先打开Anaconda prompt选定一个python环境,安装cmake和boost pip install cmake - ...

  4. Rnotebook中用python画图

    如果notebook需要转化为pdf, 能想到办法是保存图片文件,嵌入mardown语法中. 但是如果在html中显示, 可以考虑下面思虑, 比较取巧. ``` {python, engine.pat ...

  5. coursera课程视频

    #!/usr/bin/env python # coding=utf-8 import urllib import urllib2 import cookielib def setcookie(ena ...

  6. [思路笔记]WEB安全之漏洞挖掘

    记录自己在实际渗透测试以及漏洞挖掘中会用到的思路和方法.不断完善,尽量以系统的方式展现程序化式的漏洞挖掘.由于各种原因,不便公开. 通用策略 1.信息搜集 : 数据挖掘.业务挖掘 数据: 邮箱.手机号 ...

  7. 自定义字段从BOM带入生产用料清单

    自定义字段从BOM带入生产用料清单

  8. SAP PM:设备主数据常用BAPI

    如下参考: STATUS_CHANGE_EXTERN BAPI_EQMT_MODIFY BAPI_EQUI_CHANGE PM BAPI: Change Equipment BAPI_EQUI_CRE ...

  9. 【个人笔记】ximo早期发的脱壳教程——手脱UPX壳

    [个人笔记]ximo早期发的脱壳教程--手脱UPX壳   壳分为两种:压缩壳和加密壳,UPX是一种很简单的压缩壳.   手脱UPX壳: 工具:ExeinfoPE.OD 对象:rmvbfix 方法1:单 ...

  10. VerificationCodeService

    package me.zhengjie.system.domain; import lombok.AllArgsConstructor; import lombok.Data; import lomb ...