1、题目

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

2、代码实现

public class Solution {public List<String> fizzBuzz(int n) {if (n < 0) return null;List<String> list = new ArrayList<String>();for (int i = 1; i <= n; ++i) {if (i % 3 == 0 && i % 5 == 0) {list.add("FizzBuzz");}else if (i % 3 == 0 && i % 5 != 0) {list.add("Fizz");}else if (i % 3 != 0 && i % 5 == 0) {list.add("Buzz");} else {list.add(String.valueOf(i));}}return list;}
}

3、总结

我当时写的时候把

for int = 0; i <= n; ++i

如果输入i = 0就不行了。

把i = 1开始就对了,因为你看题目输出,都是从1开始,所以不要惯性思维老是从i  = 0 开始,i =0 ,i % 3 == 0 ,i % 5 ==0

所以以后写代码一定要注意i的初始位置。

LeetCode之Fizz Buzz相关推荐

  1. LeetCode 1195. Fizz Buzz Multithreaded--并发系列题目--Java 解法--AtomicInteger/CountDownLatch/CyclicBarrier

    题目地址:Fizz Buzz Multithreaded - LeetCode Write a program that outputs the string representation of nu ...

  2. leetcode 412. Fizz Buzz

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

  3. LeetCode412_412. Fizz Buzz

    LeetCode412_412. Fizz Buzz 一.描述 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果, ...

  4. LeetCode简单题之Fizz Buzz

    题目 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中: answer[i] == "FizzBu ...

  5. LeetCode 412:Fizz Buzz(3/5倍数)

    Together for a Shared future 一起向未来 今天我们看一道题目<412.FuzzBuzz>. 题目描述 给你一个整数 n ,找出从 1 到 n 各个整数的 Fiz ...

  6. LeetCode(412)——Fizz Buzz(JavaScript)

    写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 如果 n 同时是3和5的 ...

  7. 每天Leetcode 刷题 初级算法篇-数学问题-Fizz Buzz

    题目要求: 力扣题解: 代码 /*** @program: mydemo* @description: 数学-Fizz Buzz* @author: Mr.zeng* @create: 2021-02 ...

  8. iOS LeetCode ☞ Fizz Buzz

    给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中: answer[i] == "FizzBuzz& ...

  9. 【跟Leon一起刷LeetCode】412. Fizz Buzz

    Fizz Buzz Description: Write a program that outputs the string representation of numbers from 1 to n ...

最新文章

  1. 亿级流量架构实战之秒杀设计
  2. [AaronYang]那天有个小孩跟我说Js-NodeJS[AY0]-EJS
  3. linux 微信 开源,Makefile · 李光春/微信开发者工具 Linux版 - Gitee.com
  4. 电气论文实现:对大规模用户负荷曲线进行聚类
  5. 华中C语言程序简答题,华中科技大学0911年C语言程序设计试卷.doc
  6. shopex证书无法安装解决方法探讨
  7. IOS 文件读取4种方法 转字符串 和data
  8. 微软出手,蚕食JetBrains系市场?
  9. ZOJ 1586 QS Network
  10. 【codeforces 534D】Handshakes
  11. int true python_python基本数据类型,int, str, bool及相关操作
  12. useradd: cannot open /etc/passwd解决办法记录
  13. git pull git add git commit git branch git更新代码git提交git分支管理
  14. 当select查询为空
  15. python有趣小程序-Python全栈开发-有趣的小程序
  16. 怎么制作真人qq秀_真人秀---搏击赛事新风向标的机与忧
  17. 机器人研究方向的自我学习[2] Matlab机器人工具箱 欧拉角 参考书:《机器人学导论》第四版
  18. 对数坐标归一化_数据标准化/归一化方法(Normalization Method)汇总
  19. 【Python游戏】Python版本的雷霆战机游戏,最新最简单版本来了
  20. 分享解决jar包冲突问题的方法:(看了这个你就能解决所有包冲突问题!)

热门文章

  1. 如何入门.NET Core ? 推荐这10个优秀的开源项目!
  2. 微软Build2021今日召开,共同期待VS2022+.NET6!
  3. Redis 通过 RDB 方式进行数据备份与还原
  4. 【Azure Show】|第七期 特别版线上沙龙直播回顾. 嘉宾张坤段清华谭国欣柯克黄炜锵...
  5. C# 中 System.Index 结构体和 Hat 运算符(^)的全新用法
  6. 将微服务部署到 Azure Kubernetes 服务 (AKS) 实践
  7. 通过Windows Visual Studio远程调试WSL2中的.NET Core Linux应用程序
  8. 「Sqlserver」数据分析师有理由爱Sqlserver之三-最值得使有低投入高产出的Sqlserver功能...
  9. 简化 Pod 故障诊断:kubectl-debug 介绍
  10. Ocelot(三)- 服务发现