Python with recursion beats 96%

  • 0
    class Solution(object):
        def myPow(self, x, n):
            :type x: float
            :type n: int
            :rtype: float
            mem = {}
            rt =, abs(n), mem)
            if n < 0:
                return float(1)/rt
            return rt
        def do(self, x, n, mem):
            if n == 0:
                return 1
            if n == 1:
                return x
            if n/2 not in mem:
                mem[n/2] =, n/2, mem)
            if n-(n/2) not in mem:
                mem[n-(n/2)] =, n-(n/2), mem)
            return mem[n/2] * mem[n-(n/2)]

Log in to reply

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