其他分享
首页 > 其他分享> > 50. Pow(x, n)

50. Pow(x, n)

作者:互联网

 

 

class Solution {
   // T(n) = T(n/2) + O(1) = O(logn)
    public double myPow(double x, int n) {
        if (n == 0) return 1;
        if (n == -1) return 1 / x;
        double half = myPow(x, n >> 1);
        half *= half;
        // 是否为奇数
        return ((n & 1) == 1) ? (half * x) : half;
    }
}

标签:myPow,return,int,double,half,50,Pow
来源: https://blog.csdn.net/qq_39368007/article/details/120555356