实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,xn)。
官方解法:https://leetcode-cn.com/problems/powx-n/solution/powx-n-by-leetcode-solution/
解法一:快速幂 + 递归(36ms/15.1MB)
class Solution:
def myPow(self, x: float, n: int) -> float:
def quickMul(N):
if N == 0:
return 1.0
y = quickMul(N // 2)
return y * y if N % 2 == 0 else y * y * x
return quickMul(n) if n >= 0 else 1.0 / quickMul(-n)
解法二:快速幂 + 迭代(32ms/14.8MB)
class Solution:
def myPow(self, x: float, n: int) -> float:
def quickMul(N):
ans = 1.0
# 贡献的初始值为 x
x_contribute = x
# 在对 N 进行二进制拆分的同时计算答案
while N > 0:
if N % 2 == 1:
# 如果 N 二进制表示的最低位为 1,那么需要计入贡献
ans *= x_contribute
# 将贡献不断地平方
x_contribute *= x_contribute
# 舍弃 N 二进制表示的最低位,这样我们每次只要判断最低位即可
N //= 2
return ans
return quickMul(n) if n >= 0 else 1.0 / quickMul(-n)
力扣链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-medium/xwo102/