Short java AC solution


  • 1
    J
        public double myPow(double x, int n) {
            if(n==0)    return 1;
            else if(n==1)   return x;
            if(n<0&&n>Integer.MIN_VALUE) return 1/myPow(x,-n);
            else if(n==Integer.MIN_VALUE)   return 1/myPow(x*x,n>>1);
            if(n%2==0)  return myPow(x*x,n>>1);
            else    return myPow(x*x,n>>1)*x;
        }

Log in to reply
 

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