412. Fizz Buzz

网友投稿 442 2022-11-11

412. Fizz Buzz

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"

public class Solution { public List fizzBuzz(int n) { List res=new ArrayList<>(); for(int i=1;i<=n;i++){ if(i/3*3==i && i/5*5==i) res.add("FizzBuzz"); else if(i/3*3==i) res.add("Fizz"); else if(i/5*5==i) res.add("Buzz"); else res.add(Integer.toString(i)); } return

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot创建线程池的两种方式小结
下一篇:415. Add Strings
相关文章

 发表评论

暂时没有评论,来抢沙发吧~