class Solution:
def myPow(self, x: float, n: int) -> float:
if abs(x) < 1e-10:
return 0
if n < 0:
x = 1 / x
n = -n
ans = 1
tmp = x
while n != 0:
if n % 2 == 1:
ans = ans * tmp
tmp *= tmp
n //= 2
return ans
Time O(logn). Space O(1).