Problem
Given number n. Print number from 1 to n. But:
when number is divided by 3, print "fizz".
when number is divided by 5, print "buzz".
when number is divided by both 3 and 5, print "fizz buzz".
Example
If n = 15, you should return:
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz", "11", "fizz",
"13", "14", "fizz buzz"
]
Solution
class Solution {
public ArrayList<String> fizzBuzz(int n) {
ArrayList<String> results = new ArrayList<String>();
for (int i = 1; i <= n; i++) {
if (i % 15 == 0) {
results.add("fizz buzz");
} else if (i % 5 == 0) {
results.add("buzz");
} else if (i % 3 == 0) {
results.add("fizz");
} else {
results.add(String.valueOf(i));
//or Integer.valueOf(i).toString()
}
}
return results;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。