python solution that beats 77.73%


  • 1
    M
        def isEven(self, n):
            return n % 2 == 0
            
        def myPow(self, x, n):
            # base case
            if not n:
                return 1
            # n is negtive
            if n < 0:
                return 1.0 / self.myPow(x, -n)
            # n is even
            if self.isEven(n): 
                result = self.myPow(x, n / 2)
                return result * result
            else: # n is odd
                return x * self.myPow(x, n-1)
    

Log in to reply
 

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