题目描述
写一个程序,输出从 1 到 n 数字的字符串表示。
-
如果 n 是3的倍数,输出“Fizz”;
-
如果 n 是5的倍数,输出“Buzz”;
3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。
示例:
n = 15,返回:
["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"
]
很简单,想都不用想,直接编码就完了
public List<String> fizzBuzz(int n) {List<String> list = new ArrayList<String>();for(int i=1;i<=n;i++) {if(i%3!=0 && i%5!=0) {list.add(String.valueOf(i));}else {StringBuilder sb = new StringBuilder();if(i%3==0) {sb.append("Fizz");}if(i%5==0) {sb.append("Buzz");}list.add(sb.toString());}}return list;}