FizzBuzz Solution (Java)


  • 0
    W
    class Solution {
        public List<String> fizzBuzz(int n) {
            List<String> fbList = new ArrayList<String>();
            for(int i = 1; i <= n; i++){
                if(i % 3 == 0 && i % 5 == 0)
                    fbList.add("FizzBuzz");
                else if(i % 5 == 0)
                    fbList.add("Buzz");
                else if(i % 3 == 0)
                    fbList.add("Fizz");
                else
                    fbList.add(Integer.toString(i));
            }//end for
            return fbList;
        }//end method
    }//end class
    

Log in to reply
 

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