412. Fizz Buzz
来源:互联网 发布:淘宝首页自定义间隙 编辑:程序博客网 时间:2024/06/12 19:20
题目:
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"]思路:
本题题目说得很清楚,3的倍数为"Fizz",5的倍数"Buzz",15的倍数"FizzBuzz",但是要注意数组下表从0计数
代码:
class Solution {public: vector<string> fizzBuzz(int n) { vector<string> s; for(int i =0;i<n;i++) { if(!((i+1)%3)&&!((i+1)%5)) { s.push_back("FizzBuzz"); } else if(!((i+1)%3)) s.push_back("Fizz"); else if(!((i+1)%5)) s.push_back("Buzz"); else s.push_back(to_string(i+1)); } return s; }};
阅读全文
0 0
- 412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 412. Fizz Buzz
- 412. Fizz Buzz
- [LeetCode]--412. Fizz Buzz
- 412. Fizz Buzz
- LeetCode-412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 412. Fizz Buzz
- 412. Fizz Buzz
- 【leetcode】412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 412. Fizz Buzz
- 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- Leetcode 412.Fizz Buzz
- leetcode-412. Fizz Buzz
- js 全局变量优点和缺点
- 【JavaScript语言精粹】读书笔记(一)——语法
- sublimetext 超级权限,写入权限
- 互联网协议入门/this/carriage return/line feed
- (DFS)Tempter of the Bone -- HDOJ
- 412. Fizz Buzz
- 好文链接
- Html-3.<li> 标签
- Codeforces 534B Covered Path【有点奇怪的思维+贪心】
- php csrf防御
- Tab切换--JavaScript基础
- JAVA虚拟机 类加载机制理解
- Android 多线程编程初探
- day_03_QT设计师、创造器