Java Solution without hardcoding or recursion


  • 0
    U
    public boolean isPowerOfThree(int n) {
            
            if(n>0){
                 //this calculates log(n) to the base 3 == log(n)/log(3)
                double numerator = Math.log(n);
                double denom = Math.log(3);
                int value = (int) Math.round(numerator/denom);
               //raise the result of log to 3 and it should equal n
                if(Math.pow(3,value)==n)
                    return true;
                
            }
                
            return false;
        }
    

Log in to reply
 

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