letecode [412] - Fizz Buzz

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 = 15,

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

题目大意

  给定n,当1~n中的数为3、5、15的倍数时输出字符串“Fizz”、“Buzz”、“FizzBuzz”,否则直接输出数字的字符串形式。

理  解:

  vector数组保存1~n的所有字符串形式。替换掉3,5,15的倍数对应的值。

代 码 C++:

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> res;
        for(int i=1;i<=n;++i){
            res.push_back(to_string(i));
        }
        int indexOfThree=1,indexOfFive=1,indexOfFifteen=1;
        while(3*indexOfThree<=n){
            res[3*indexOfThree-1] = "Fizz";
            indexOfThree++;
        }
        while(5*indexOfFive<=n){
            res[5*indexOfFive-1] = "Buzz";
            indexOfFive++;
        }
        while(15*indexOfFifteen<=n){
            res[15*indexOfFifteen-1] = "FizzBuzz";
            indexOfFifteen++;
        }
        return res;
    }
};

运行结果:

  执行用时 :8 ms, 在所有 C++ 提交中击败了97.43%的用户

  内存消耗 :10.1 MB, 在所有 C++ 提交中击败了82.09%的用户

原文地址:https://www.cnblogs.com/lpomeloz/p/11066880.html

时间: 2024-08-14 04:10:09

letecode [412] - Fizz Buzz的相关文章

412. Fizz Buzz

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 fiv

LeetCode 412. Fizz Buzz

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 bot

LeetCode之412. Fizz Buzz

-------------------------------------------- 虽然是从最简单的开始刷起,但木有想到LeetCode上也有这么水的题目啊... AC代码: public class Solution { public List<String> fizzBuzz(int n) { List<String> res=new ArrayList<>(); for(int i=1;i<=n;i++){ if(i/3*3==i &&

LeetCode - 412. Fizz Buzz - ( c++ ) - 解题报告

1.题目大意 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

Lintcode 9.Fizz Buzz 问题

------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++

LintCode (9)Fizz Buzz

下面是AC代码,C++风格: 1 class Solution { 2 public: 3 vector<string> fizzBuzz(int N) { 4 vector<string> Answer; 5 for(int i = 1;i <= N;i++) { 6 if(i % 15 == 0) { 7 Answer.push_back("fizz buzz"); 8 } else if(i % 3 == 0) { 9 Answer.push_bac

Lintcode: Fizz Buzz

Given number n. Print number from 1 to n. But: when number is divided by 3, print fizz; when number is divided by 5, print buzz; when number is divided by both 3 and 5, print fizz buzz. Example If n=15, you should return ["1", "2", &qu

Lintcode9 Fizz Buzz solution 题解

[题目描述] Given number n. Print number from 1 to n. But: when number is divided by 3, print "fizz". when number is divided by 5, print "buzz". when number is divided by both 3 and 5, print "fizz buzz". 给你一个整数n. 从 1 到 n 按照下面的规则打印

Fizz Buzz 问题

Fizz Buzz 问题 给你一个整数n. 从 1 到 n 按照下面的规则打印每个数: 如果这个数被3整除,打印fizz. 如果这个数被5整除,打印buzz. 如果这个数能同时被3和5整除,打印fizz buzz. 样例 比如 n = 15, 返回一个字符串数组: [ "1", "2", "fizz", "4", "buzz", "fizz", "7", "