剑指offer-数值的整数次方12

时间:2024-07-23 00:03:08
class Solution:
def Power(self, base, exponent):
# write code here
if base==0:
return 0
if exponent==0:
return 1
e=abs(exponent)
tmp=base
res=1
while e>0:
if e&1==1:
res*=tmp
e=e>>1
tmp=tmp*tmp
if exponent>0:
return res
else:
return 1/res

采用快速幂算法https://www.cnblogs.com/CXCXCXC/p/4641812.html