[LeetCode] 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"
]

这道题真心没有什么可讲的,就是分情况处理就行了。

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> res;
        for (int i = 1; i <= n; ++i) {
            if (i % 15 == 0) res.push_back("FizzBuzz");
            else if (i % 3 == 0) res.push_back("Fizz");
            else if (i % 5 == 0) res.push_back("Buzz");
            else res.push_back(to_string(i));
        }
        return res;
    }
};

本文转自博客园Grandyang的博客,原文链接:嘶嘶嗡嗡[LeetCode] Fizz Buzz ,如需转载请自行联系原博主。

相关文章
|
7月前
【Leetcode -412.Fizz Buzz -414.第三大的数】
【Leetcode -412.Fizz Buzz -414.第三大的数】
31 0
LeetCode 412. Fizz Buzz
写一个程序,输出从 1 到 n 数字的字符串表示。
129 0
LeetCode 412. Fizz Buzz
LeetCode 412. Fizz Buzz
|
算法 Java C#
【算法千题案例】每日LeetCode打卡——74.Fizz Buzz
📢前言 🌲原题样例:Fizz Buzz 🌻C#方法:模拟 + 字符串拼接 🌻Java 方法:模拟 + 字符串拼接 💬总结
【算法千题案例】每日LeetCode打卡——74.Fizz Buzz
|
算法 Python
<LeetCode天梯>Day043 Fizz Buzz(按部就班) | 初级算法 | Python
<LeetCode天梯>Day043 Fizz Buzz(按部就班) | 初级算法 | Python
<LeetCode天梯>Day043 Fizz Buzz(按部就班) | 初级算法 | Python
|
算法 文件存储
[leetcode/lintcode 题解] 算法面试真题详解:Fizz Buzz 问题
[leetcode/lintcode 题解] 算法面试真题详解:Fizz Buzz 问题
[leetcode/lintcode 题解] 算法面试真题详解:Fizz Buzz 问题
[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
1016 0
|
Java
LeetCode 412 Fizz Buzz
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/52842179 翻译 写一个程序,其输出表示数字1到n。
752 0
|
3天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
7 0