[LeetCode] 412.Fizz Buzz
来源:互联网 发布:女生黄金比例身材算法 编辑:程序博客网 时间:2024/06/05 21:55
[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 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”
]
解题思路
只要弄清楚题目意思这道题就很容易能解决,唯一一点就是注意vector容器,类型为string,在除了被3整除和被5整除以及被15整除三种情况外,其他的数字也要转换为字符串类型压入vector,我这里用到了to_string()函数解决了这个问题。
实验代码
class Solution {public: vector<string> fizzBuzz(int n) { vector<string> v; for (int i = 1; i <= n; i++) { if ((i % 3 == 0) && (i % 5 == 0)) v.push_back("FizzBuzz"); else if (i % 3 == 0) v.push_back("Fizz"); else if (i % 5 == 0) v.push_back("Buzz"); else v.push_back(to_string(i)); } return v; }};
阅读全文
0 0
- LeetCode 412. Fizz Buzz
- [LeetCode]--412. Fizz Buzz
- LeetCode-412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 【leetcode】412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- leetcode 412. Fizz Buzz
- Leetcode 412.Fizz Buzz
- leetcode-412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- 【LeetCode】 412. Fizz Buzz
- LeetCode 412. Fizz Buzz
- LeetCode--412. Fizz Buzz
- 412. Fizz Buzz [LeetCode]
- [leetcode]--412. Fizz Buzz
- Leetcode-412. Fizz Buzz
- [LeetCode]412. Fizz Buzz
- 忘记oracle的sys用户密码怎么修改
- [LeetCode] 371.Sum of Two Integers
- ubuntu下配置git和github
- C#--程序登录时写入登录日志
- 波雷费密码 Playfair Cipher 及 希尔密码 Hill Cipher 原理简述
- [LeetCode] 412.Fizz Buzz
- JQuery小结
- eclipse配置JDK
- SSH与SSM学习之hibernate09——对象的三种状态
- C#--记录用户程序退出时间日志
- Java基础之集合框架--Collections.reverse()方法
- java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver解决方案
- Android移动开发-属性动画的实现
- activeMQ发布订阅模式中中常用工具类