50. Pow(x, n)

Implement pow(xn), which calculates x raised to the power n (i.e. xn).

 

方法一:

递归

 

public double quickMul(double x, long N) {
        if (N == 0) {
            return 1.0;
        }
        double y = quickMul(x, N / 2);
        return N % 2 == 0 ? y * y : y * y * x;
    }

    public double myPow(double x, int n) {
        long N = n;
        return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
    }

 

参考链接:

https://leetcode.com/problems/powx-n/

https://leetcode-cn.com/problems/powx-n/

posted @ 2020-12-23 14:12  diameter  阅读(56)  评论(0编辑  收藏  举报