题目:Implement pow(x, n).
两个简化运算的地方,一是递归计算,而是提取出x^2计算
class Solution:
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if(n==0):
return 1.0
if(n<0):
x = 1/x
n = abs(n)
x0 = x*x
return self.myPow(x0, n//2) if(n%2 == 0) else x*self.myPow(x0, n//2)
效果