java solution


  • 0
    D

    pow use java solution

    public double myPow(double x, int n) {
            int num = Math.abs(n);
            double ret = power(x, num);
            if (n < 0) {
                ret = 1 / ret;
            }
            return ret;
        }
    
        public double power(double x, int n) {
            if (n == 0) {
                return 1;
            }
            double tmp = power(x, n / 2);
            double ret;
            if ((n & 1) == 1) {
                ret = tmp * tmp * x;
            } else {
    
                ret = tmp * tmp;
            }
            return ret;
        }
    

Log in to reply
 

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