Wordy but fastest Java Solution


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

Log in to reply
 

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