leetcode 50. Pow(x, n) - 快速幂
文章目录
https://leetcode-cn.com/probl...
class Solution:
def myPow(self, x: float, n: int) -> float:
if n < 0:
return 1 / self.myPow(x, -n)
if n == 0:
return 1
if n == 1:
return x
if n == 2:
return x * x
ans = self.myPow(x, n >> 1)
ans = ans * ans
if n % 2 == 1: ans *= x
return ans

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论