LeetCode412Fizz Buzz
写一个程序,输出从 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" ]
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> res;
for(int i = 1; i <= n; i++)
{
if(i % 3 == 0 && i % 5 == 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;
}
};
LeetCode412Fizz Buzz的更多相关文章
- [LeetCode] Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- 412. Fizz Buzz
https://leetcode.com/problems/fizz-buzz/ 没什么好说的,上一个小学生解法 class Solution(object): def fizzBuzz(self, ...
- Lintcode 9.Fizz Buzz 问题
------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of s ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- LeetCode之412. Fizz Buzz
-------------------------------------------- 虽然是从最简单的开始刷起,但木有想到LeetCode上也有这么水的题目啊... AC代码: public cl ...
- LeetCode Fizz Buzz
原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...
- Buzz words
给你一个字符串和字典,从头扫到位,如果到当前的字符的字符串存在于字典中,则显示 buzz. 例子: ILOVEPINEAPPLEJUICE 字典: [pine, apple, pineapple, j ...
- Fizz Buzz
class Solution { public: /** * param n: As description. * return: A list of strings. */ vector<st ...
- LintCode (9)Fizz Buzz
下面是AC代码,C++风格: class Solution { public: vector<string> fizzBuzz(int N) { vector<string> ...
随机推荐
- IoGetTopLevelIrp
学习写驱动,其实,挺无聊,但是也挺有意思的 IoGetTopLevelIrp 今天在看一个文件系统过滤驱动的时候,看到这个函数,它是干嘛的,为什么会有这么个东西 https://msdn.micros ...
- Java开发系列-时间转换
获取当前时间戳 // 获取当前的时间戳 long time = new Date().getTime(); 将字符串时间戳转成格式的时间字符串 Long timestrap = Long.parseL ...
- 2019-8-31-dotnet-获取程序所在路径的方法
title author date CreateTime categories dotnet 获取程序所在路径的方法 lindexi 2019-08-31 16:55:58 +0800 2019-03 ...
- Educational Codeforces Round49
A Palindromic Twist(字符串) 问每个字母必须向左或向右变成另一个字母,问能不能构成回文 #include <iostream> #include <string. ...
- 洛谷P3834【模板】可持久化线段树 1(主席树)
题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...
- 使用phpStudy自带的mysql-front学习建库建表以及基本的mysql语句
1.鼠标左键phpStudy图标,点击mysql管理器,如下图 2.选择Mysql-Front,选择localhost进入,可以看到本地建立的数据库.然后新建一个数据库,如下图: 3.在新建的数据库上 ...
- js图片预加载实现!
var myImage = (function(){ var imgNode = document.createElement( 'img' ); document.body.appendChild( ...
- 菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表ngx_chain_t[转]
菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...
- 怎么解决VirtualBox无法安装增强工具
点击「设备」-「安装增强功能」,然后就弹出下面这个东西,百度和 bing 了很久,终于解决啦~ Unable to insert the virtual optical disk D:\Program ...
- filterBuilders 构建过滤器query
FilterBuilders构建过滤器Query package com.elasticsearch; import org.elasticsearch.action.ActionListener; ...