实现 pow(x,n)
不用担心精度,当答案和标准输出差绝对值小于1e-3时都算正确
样例
Pow(2.1, 3) = 9.261
Pow(0, 1) = 0
Pow(1, 0) = 1
挑战
O(logn) time
class Solution:
"""
@param x: the base number
@param n: the power number
@return: the result
"""
def myPow(self, x, n):
if n==0:
return 1
if n<0:
return 1/self.myPow(x,-n)
elif n%2==0:
return self.myPow(x*x,n//2)
elif n%2==1:
return x*self.myPow(x*x,n//2)