Other way of Doing it.


  • 0
    H

    This is O(n), however I tried to minimize the String append and Modulo operations.

     public List<String> fizzBuzz(int n) {
            String[] arr= new String[n];
            for(int i=1;i<=n;i++){
                if(arr[i-1]==null)
                    arr[i-1]=""+i;;
                    
                
                if(i*3<=n && arr[3*i-1]==null)
                    arr[3*i-1]="Fizz";
                if(i*5<=n && arr[5*i-1]==null)
                    arr[5*i-1]="Buzz";  //FizzBuzz
                if(i*15<=n && arr[15*i-1]==null)
                    arr[15*i-1]="FizzBuzz";
            }
            
            return  Arrays.asList(arr);
        }
    

Log in to reply
 

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