Iteration with mod / T : O(N), S : O(N)


  • 0
    J
    public List<String> fizzBuzz(int n) {
            List<String> lst = new ArrayList<String>(); 
            
            for(int i = 1 ; i <= n; i++){
                if(i%3 != 0 && i%5 != 0){
                    lst.add(String.valueOf(i));
                }
                else{
                    String result = (i%3 == 0) ? "Fizz" : "";
                    if(i%5 == 0) result += "Buzz";
                    lst.add(result);
                }
            }
            
            return lst;
        }
    

Log in to reply
 

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