My 10-line Java solution using recursion


  • 0
    H
    public class Solution {
        public double pow(double x, int n) {
            if(n == 0) return 1;
            if(n < 0){
                x = 1 / x;
                n = -n;
            }
            double res = pow(x, n/2);
            if(n % 2 == 0) return res*res;
            else return res*res*x;
        }
    }

  • 0
    W

    I am wondering, OJ didn't check for Integer.MAX_VALUE overflow?


Log in to reply
 

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