快速幂
Java
class Solution {
public double myPow(double x, int n) {
if (n == 0) return 1.0;
long m = (long)n; // 这里因为Integer.MIN_VALUE,INT_MIN,前面加负号还是本身,所以用long存
return m > 0 ? qmi(x, m) : 1 / qmi(x, -m);
}
public double qmi(double x, long n) {
double res = 1;
while (n > 0) {
if ((n & 1) == 1) res *= x;
n >>= 1;
x = x * x;
}
return res;
}
}