50. Pow(x, n)

二刷。。

二刷有印象,所以一下做出来了,不过还是忽略了MIN_VALUE的问题。。。
还是divide and conquer

public class Solution 
{
    public double myPow(double x, int n) 
    {
        if(n == 0) return 1.0;
        
        if(n == Integer.MIN_VALUE) return helper(1/x,Integer.MAX_VALUE) * 1/x;
        
        if(n < 0) return 1.0/helper(x,-n);
        else return helper(x,n);
    }
    
    public double helper(double x, int n)
    {
        if(n == 0) return 1.0;
        
        double temp = helper(x,n/2);
        temp *= temp;
        if(n%2!=0) temp *= x;
        
        return temp;
    }
}

刚开始刷的时候这种题很伤脑筋。。

原文地址:https://www.cnblogs.com/reboot329/p/5951369.html