A recursive solution in Python

  • 0
    class Solution(object):
        def myPow(self, x, n):
            :type x: float
            :type n: int
            :rtype: float
            def pow(x, n):
                if n == 1:
                    return x
                tmp = pow(x, n//2)
                return tmp*tmp if n%2 == 0 else tmp*tmp*x
            if n == 0:
                return 1
            res = pow(x, abs(n))
            return res if n>=0 else 1/res

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.