当前位置:网站首页>LeetCode FizzBuzz C#解答
LeetCode FizzBuzz C#解答
2022-07-04 17:39:00 【charlsdm】
- Fizz Buzz
给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中:
answer[i] == “FizzBuzz” 如果 i 同时是 3 和 5 的倍数。
answer[i] == “Fizz” 如果 i 是 3 的倍数。
answer[i] == “Buzz” 如果 i 是 5 的倍数。
answer[i] == i (以字符串形式)如果上述条件全不满足。
示例 1:
输入:n = 3
输出:[“1”,“2”,“Fizz”]
示例 2:
输入:n = 5
输出:[“1”,“2”,“Fizz”,“4”,“Buzz”]
示例 3:
输入:n = 15
输出:[“1”,“2”,“Fizz”,“4”,“Buzz”,“Fizz”,“7”,“8”,“Fizz”,“Buzz”,“11”,“Fizz”,“13”,“14”,“FizzBuzz”]
提示:
1 <= n <= 104
通过次数132,841提交次数187,101
请问您在哪类招聘中遇到此题?
附上我的AC C#代码
public class Solution
{
public List<string> FizzBuzz(int n)
{
List<string> mylist = new List<string>();
for(int i=0;i<n;i++)
{
mylist.Add((i + 1).ToString());
}
for(int i=0;i<mylist.Count;i++)
{
int temp = int.Parse(mylist[i]);
if(temp%3==0&&temp%5==0)
{
mylist[i] = "FizzBuzz";
}
else if(temp%3==0)
{
mylist[i] = "Fizz";
}
else if(temp%5==0)
{
mylist[i] = "Buzz";
}
}
return mylist;
}
}
边栏推荐
- Send and receive IBM WebSphere MQ messages
- Scala基础教程--13--函数进阶
- 字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
- [uniapp] uniapp development app online Preview PDF file
- Li Kou brush question diary /day4/6.26
- 性能优化之关键渲染路径
- Wanghongru research group of Institute of genomics, Chinese Academy of Agricultural Sciences is cordially invited to join
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- Li Kou brush question diary /day3/2022.6.25
- 一、C语言入门基础
猜你喜欢
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
Scala基础教程--15--递归
Scala基础教程--16--泛型
力扣刷题日记/day1/2022.6.23
Li Kou brush question diary /day3/2022.6.25
输入的查询SQL语句,是如何执行的?
性能优化之关键渲染路径
Wireshark抓包TLS协议栏显示版本不一致问题
随机推荐
中国农科院基因组所汪鸿儒课题组诚邀加入
6.26cf simulation match B: solution to array reduction problem
基于lex和yacc的词法分析器+语法分析器
利用策略模式优化if代码【策略模式】
ThreadLocal原理与使用
Caché WebSocket
国元期货是正规平台吗?在国元期货开户安全吗?
力扣刷题日记/day2/2022.6.24
[release] a tool for testing WebService and database connection - dbtest v1.0
【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
问下各位大佬有用过cdc直接mysql to clickhouse的么
Don't just learn Oracle and MySQL!
Go microservice (II) - detailed introduction to protobuf
读写关闭的channel是啥后果?
神经网络物联网是什么意思通俗的解释
力扣刷题日记/day1/2022.6.23
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
IBM WebSphere MQ检索邮件