直接上快速幂
原题链接:https://www.acwing.com/problem/content/description/26/
class Solution {
public:
double Power(double base, int exponent) {
typedef long long LL;
bool is_minus = exponent < 0;
double res = 1;
for(LL k = abs(LL(exponent)); k; k >>= 1)
{
if(k & 1) res *= base;
base *= base;
}
if(is_minus) res = 1 / res;
return res;
}
};
class Solution(object):
def Power(self, base, exponent):
"""
:type base: float
:type exponent: int
:rtype: float
"""
is_minus = exponent < 0
res = 1
k = abs(exponent)
while k:
if k & 1:
res *= base
base = base * base
k >>= 1
if is_minus:
res = 1 / res
return res