My Java solution


  • 2
    S
    public double pow(double x, int n) {
            if(n == 0)
                return 1;
            if(n == 1)
                return x;
            if(n%2 == 0)
                return pow(x*x, n/2);
            else{
                if(n>0)
                    return x*pow(x*x, n/2);
                else
                    return pow(x*x, n/2)/x;
            } 
        }

Log in to reply
 

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