剑指 Offer 16. 数值的整数次方
快速幂。
class Solution {
public:
double qpow(double x, long long n) {
double res = 1;
while (n) {
if (n & 1) res *= x;
x = x * x;
n >>= 1;
}
return res;
}
double myPow(double x, int n) {
return n >= 0 ? qpow(x, n) : 1.0 / qpow(x, -(long long)n);
}
};