50. Pow(x, n)
Implement pow(x, n), 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/