博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 412. Fizz Buzz
阅读量:4445 次
发布时间:2019-06-07

本文共 950 字,大约阅读时间需要 3 分钟。

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"]
class Solution {    public List
fizzBuzz(int n) { List
res = new ArrayList<>(); for(int i = 1; i <= n; i++) { if(i % 15 == 0) { res.add("FizzBuzz"); }else if(i % 3 == 0){ res.add("Fizz"); }else if(i % 5 == 0){ res.add("Buzz"); }else { res.add(i + ""); } } return res; }}

 

转载于:https://www.cnblogs.com/jamieliu/p/10402512.html

你可能感兴趣的文章
敏捷开发流程
查看>>
APP兼容性测试(三)测试方案设计
查看>>
React的性能优化 - 代码拆分之lazy的使用方法
查看>>
在canvas中使用其他HTML元素
查看>>
React的新特性 ---- Hooks ---- 的基本使用
查看>>
History Introduction to Mining Industry of Czech
查看>>
富文本框
查看>>
动态网页开发基础
查看>>
mysql 恢复备份
查看>>
LeetCode-Create Maximum Number
查看>>
随想之三 -CMDB
查看>>
STM32的DMA
查看>>
Process Class (System.Diagnostics)
查看>>
hdu 3001
查看>>
手机端H5上滑加载下一页
查看>>
Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts
查看>>
Spring框架中Bean管理的常用注解
查看>>
Core Animation系列之CADisplayLink
查看>>
dedecms标签调用大全
查看>>
《与小卡特一起学Python》Code1
查看>>