【LeetCode】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”.
【注意】是从1开始的,所以循环的时候 i = 1; 然后还有正常+的时候是String所以要用到Integer.toString()
#code
class Solution {
public List fizzBuzz(int n) {
List<String> a = new ArrayList<>();
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0) {
a.add("FizzBuzz");
}else if(i%5==0){
a.add("Buzz");
}else if(i%3==0){
a.add("Fizz");
}else{
a.add(Integer.toString(i));
}
}
return a;
}
}