use plus only


  • 0
    D
    public List<String> fizzBuzz(int n) {
           List<String> ans = new ArrayList<>();
           int a = 0, b = 0;
           for (int i = 1; i < n + 1; i++) {
               a++;
    	   b++;
    	    if (a == 3 && b == 5) {
    	        ans.add("FizzBuzz");
    		b = 0;
    		a = 0;
    	    } else if (a == 3) {
    		ans.add("Fizz");
    		a = 0;
    	   } else if (b == 5) {
    		ans.add("Buzz");
    		b = 0;
    	  } else {
    		ans.add(String.valueOf(i));
    	 }
       }
    	return ans;
     }
    

Log in to reply
 

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