注意负数的情况,将基数和次方处理一下
这里使用分治减少一半复杂度
class Solution {
public:
double Power(double base, int exponent) {
if (exponent < 0) {
base = 1 / base;
exponent = -exponent;
}
double res = 1;
for (int i = 1; i <= exponent / 2; ++i) {
res *= base;
}
res *= res;
if (exponent % 2) {
res *= base;
}
return res;
}
};