【递归】快速幂

1709255340379.webp
class Solution:
    def myPow(self, x: float, n: int) -> float:
        if n==0:
            return 1
        if n==1:
            return x
        if n==-1:
            return 1/x
        y = self.myPow(x, n>>1)
        ans = y * y
        if (n%2==1):
            ans *=x
        return ans
© 版权声明
THE END
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容