This works


  • 0
    B

    Re: Short and easy to understand solution
    following code passes case of '2.0000, Integer.MIN_VALUE'.
    public double myPow(double x, int n) {
    if(x == 0) return 0;
    if(n == 0) return 1;

        if(n == 0) return 1;
        
        
        if(n < 0){
            n = -n;
            
            if(n >= 0)//need to check if it's MIN_VALUE, if it is, don't flip yet. Next recursion will do.
                x = 1 / x;
        }
        
        return (n % 2 == 0 ? 1 : x) * myPow(x * x, n / 2);
    }

Log in to reply
 

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