[LeetCode] 50. Pow(x, n)

方法一:快速幂 + 递归

package leetcode;

/**
 * @author doyinana
 * @create 2020-05-11 22:33
 */
public class L50 {
    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);
    }
}

 

posted @ 2020-05-11 23:11  doyi  阅读(87)  评论(0编辑  收藏  举报