Simple Java Solution O(n) in 6ms


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

Log in to reply
 

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