My Java AC solution


  • 0
    public List<String> fizzBuzz(int n) {
            List<String> result = new ArrayList<String>();
            for (int i = 1; i <= n; i ++){
                if (i % 3 == 0 && i % 5 == 0){
                    result.add("FizzBuzz");
                }else if (i % 3 == 0){
                    result.add("Fizz");
                }else if (i % 5 == 0){
                    result.add("Buzz");
                }else{
                    result.add(String.valueOf(i));
                }
            }
            return result;
        }
    // Time: O(n), Space: O(1) exclude output List
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.