FizzBuzz Solution (Java)


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

Log in to reply
 

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