Java | Leetcode Java题解之第412题Fizz Buzz
题目:
题解:
class Solution {
public List<String> fizzBuzz(int n) {
List<String> answer = new ArrayList<String>();
for (int i = 1; i <= n; i++) {
StringBuffer sb = new StringBuffer();
if (i % 3 == 0) {
sb.append("Fizz");
}
if (i % 5 == 0) {
sb.append("Buzz");
}
if (sb.length() == 0) {
sb.append(i);
}
answer.add(sb.toString());
}
return answer;
}
}