class Solution {
public:
double Power(double base, int exponent) {
if(exponent == 0) return 1;
if(exponent == 1) return base;
int x = exponent / 2;
if(x > 0) {
if(x % 2 == 0) return Power(base * base, x);
else return Power(base * base, x) * base;
}
else if(x < 0){
x = -x;
if(x % 2 == 0) return 1/Power(base * base, x);
else return 1/(Power(base * base, x) * base);
}
else
return 1;
}
};
剑指offer 数值的整数次方
最新推荐文章于 2022-05-02 08:00:00 发布