Java Code Accep


  • 0
    A

    Easy java solution.

    public static List<String> fizzBuzz(int n) {
            List<String> result = new ArrayList<>();
    
            if (n == 1) {
                result.add("1");
                return result;
            }
    
            for (int i = 1; i<=n ; i++)
            {
                if (i%15 == 0)
                    result.add("FizzBuzz");
                else if ((i%3==0) || (i%5==0)) {
                    if (i%3 == 0)
                        result.add("Fizz");
                    else if (i%5 == 0)
                        result.add("Buzz");
                }
                else
                    result.add(Integer.toString(i));
    
            }
            return result;
        }
    

Log in to reply
 

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