题目描述

写一个程序,输出从 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. Excel VBA发送Email时自动允许Outlook安全对话框

    在Outlook的宏安全性设置如果选择了“为所有宏提供通知” 并且,在[编程访问]中选择了“总是向我发出警告” 在其他VBA中创建邮件过程中,如果修改Recipients或者执行Send方法,都会弹出 ...

  2. Android studio 3.0安装与配置(看这一篇就够了)

    前言 为了完成数据库大作业,并充分利用学过的Java语言,决定开发一个简单完整成熟的安卓手机应用程序.于是下载安装Android Studio集成开发环境,第一次安装最新版本,因为墙的原因安装失败,第 ...

  3. 洛谷P1435 回文子串

    题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “A ...

  4. Debian8.8下的VIM的配置文件

    传动们:http://blog.csdn.net/gatieme/article/details/43883261?spm=5176.100239.blogcont47532.3.yXiEuB 感觉挺 ...

  5. TPO1-3Timberline Vegetation on Mountains

    At the upper timberline the trees begin to become twisted and deformed. This is particularly true fo ...

  6. DNA methylation|Transcription factors|PTM|Chromosome conformation|表观遗传学测序技术

    生物医疗大数据-DNA element functions and identification Genetic vs epigenetic GENETICS  遗传学 DNA Code: 64 tr ...

  7. 关于前端CSS的总结

    CSS语法 CSS语言的基本单位是样式声明:propertyName : value ; CSS语言的使用方式: 1.把CSS样式声明作为HTML标签的style属性值.2.使用CSS选择器 CSS常 ...

  8. HDU-3974 Assign the task(多叉树DFS时间戳建线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Time Limit: 15000/5000 MS (Java/Others)    Memory Lim ...

  9. Exynos4412开发板-网络-同一网段

    1.1 同一网段在不少实验中,都会需要用到局域网的一些基础知识,在技术支持的过程中,发现不少用户对于这个概念非常模糊,导致 IP 地址或者网络环境稍微有点变化,就无法实现实验.如果没有接触过这个概念, ...

  10. 系统学习Javaweb5----JavaScript1

    注意:java和JavaScript没有半毛钱关系!!! 说明:过年歇着歇着不知不觉就歇了七天,嘿嘿,从今天开始继续学习. 学习笔记: 1.JavaScript概述. 1.1.JavaScript是什 ...