Java solution with 3ms


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

Log in to reply
 

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