import java.util.ArrayList; import java.util.List; class Solution { public List<String> fizzBuzz(int n) { List<String> answer = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 == 0) { answer.add("FizzBuzz"); } else if (i % 3 == 0) { answer.add("Fizz"); } else if (i % 5 == 0) { answer.add("Buzz"); } else { answer.add(String.valueOf(i)); // Convert integer i to a string } } return answer; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter