Java logn solution


  • 0
    L
    public double myPow(double x, int n) {
            if(n==0) {
                return 1.0;
            }
            else if(n==1) {
                return x;
            }
            else if(n==-1) {
                return 1/x;
            }
            
            if(n%2==0) {
                double half = myPow(x,n/2);
                return half*half;
            }
            else {
                double half = myPow(x,(n-1)/2);
                return half*half*x;
            }
        }
    

Log in to reply
 

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