题目描述

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

  1. 1. 如果 n 3的倍数,输出“Fizz”;
  2. 2. 如果 n 5的倍数,输出“Buzz”;
  3. 3. 如果 n 同时是35的倍数,输出 FizzBuzz”。

示例:

  1. n = 15,
  2. 返回:
  3. [
  4. "1",
  5. "2",
  6. "Fizz",
  7. "4",
  8. "Buzz",
  9. "Fizz",
  10. "7",
  11. "8",
  12. "Fizz",
  13. "Buzz",
  14. "11",
  15. "Fizz",
  16. "13",
  17. "14",
  18. "FizzBuzz"
  19. ]

题目分析

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

源码

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
  1. 大专栏  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. 面向对象 part6 继承

    继承 js实现的是实现继承/也就是继承实际的方法 //主要依赖:原型链 //基本思路: 就是一个引用类型继承另一个引用类型的属性和方法 详细:构造函数,实例,原型之间的关系.每个构造函数都有一个原型对 ...

  2. Tensorflow学习教程------读取数据、建立网络、训练模型,小巧而完整的代码示例

    紧接上篇Tensorflow学习教程------tfrecords数据格式生成与读取,本篇将数据读取.建立网络以及模型训练整理成一个小样例,完整代码如下. #coding:utf-8 import t ...

  3. springboot配置多个yml文件

    新接触了springboot项目,yml一大堆,启动不知道用的哪个,各种百度后: <profiles> <profile> <id>dev</id> & ...

  4. NWERC 2015

    2015-2016 Northwestern European Regional Contest (NWERC 2015) F H没做 似乎只有 B 题有点意思 D:数论分块枚举所有上取整区间,只需要 ...

  5. java生成图形验证码

    效果图 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buf ...

  6. linux安装nginx步骤

    转载自:https://blog.csdn.net/t8116189520/article/details/81909574,修改部分内容 本文已收录至博客专栏linux安装各种软件及配置环境教程中 ...

  7. YOLO配置文件理解

    [net] batch=64 每batch个样本更新一次参数. subdivisions=8 如果内存不够大,将batch分割为subdivisions个子batch,每个子batch的大小为batc ...

  8. std::string和ctime之间的转换

    int year, month, day, hour, minute, second; string strTime: sscanf(strTime.c_str(), "%d-%d-%d % ...

  9. druid+mybaits简单集成

    在前面的文章中,我们对springboot开发中一些常用的框架进行了集成,但是发现还是存在一些问题,比如druid还需要比较长的固有配置,实际上druid官方是提供了相关的starters包的,内部采 ...

  10. nc命令的用法

    1.什么是nc netcat(nc)是一个简单而有用的工具,可以使用tcp或者udp进行网络间读写数据,传输文件,接收发送数据,验证网络是否畅通. 2.命令行: 1) -l 用于指定nc将处于侦听模式 ...