题解 | #数值的整数次方#
数值的整数次方
http://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00
非递归的快速幂/位运算
class Solution:
def Power(self , base: float, exponent: int) -> float:
if exponent<0: # 如果幂小于0
exponent = - exponent
base = 1/base
in_x = base
res = 1
while exponent!=0:
if exponent&1:
res = res*in_x
in_x = in_x*in_x # 幂运算
exponent=exponent>>1 #向右移位
return res