1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public List<String> fizzBuzz(int n) { List<String> resultList = new ArrayList<>(n);
for (int i = 1; i <= n; i++) { boolean mod3 = i % 3 == 0; boolean mod5 = i % 5 == 0; if (mod3 && mod5) { resultList.add("FizzBuzz"); } else if (mod3) { resultList.add("Fizz"); } else if (mod5) { resultList.add("Buzz"); } else { resultList.add(String.valueOf(i)); } }
return resultList; } }
|