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;
    }
}

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

posted @ 2016-10-12 02:19  哇呀呀..生气啦~  阅读(98)  评论(0编辑  收藏  举报