题目描述

写一个程序,输出从 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. c语言中常用的串运算

    调用标准库函数 #include<string.h> 串比较,strcmp(char s1,char s2) 串复制,strcpy(char to,char from) 串连接,strca ...

  2. JavaSE--Comparator 和 Comparable 区别

    public interface Comparable<T> 此接口强行对实现它的每个类的对象进行整体排序.这种排序被称为类的自然排序,类的 compareTo 方法被称为它的自然比较方法 ...

  3. \_\_doc\_\_

    __doc__ 注释信息 一.doc 返回类的注释信息 class Foo: '我是描述信息' pass print(Foo.__doc__) 我是描述信息 该属性无法被继承 class Foo: ' ...

  4. 两种大小写比较|elif|

    name = ['alle','mike','tom','jerry','alice','hebe'] for i in name: if i == 'tom': print 'get!' #get! ...

  5. Hbase的极限测试经验之java项目的jar包导入

    Hbase的极限测试的内容是把之前编过的网站的后台数据库改成hbase即可. 我很快就在hbase数据库中建完表,也把关于操作数据库的函数写好了. 当我调试时,发现在jsp中的操作数据库的函数都不能用 ...

  6. 1)关于配置centos的网络问题

    网址: http://jingyan.baidu.com/article/f3e34a12d886d2f5eb653515.html

  7. Gson、jackson 序列化,反序列化(单个、集合)

    实体类: package com.nf.redisDemo1.entity; public class News { private long id; private String title; pr ...

  8. hibernate中lazy的使用

    lazy,延迟加载 Lazy的有效期:只有在session打开的时候才有效:session关闭后lazy就没效了. lazy策略可以用在: * <class>标签上:可以取值true/fa ...

  9. EntityNotFoundException EntityExistException

    package me.zhengjie.common.exception; import org.springframework.util.StringUtils; import java.util. ...

  10. 调参、最优化、ml算法(未完成)

    最优化方法 调参方法 ml算法 梯度下降gd grid search lr 梯度上升 随机梯度下降 pca 随机梯度下降sgd  贝叶斯调参 lda 牛顿算法   knn 拟牛顿算法   kmeans ...