Python solution - O(logn) 59ms


  • 1
    G
    class Solution:
        # @param {float} x
        # @param {integer} n
        # @return {float}
        def myPow(self, x, n):
            if n < 0:
                return 1 / self.pow(x, -n)
            else:
                return self.pow(x, n)
    
        def pow(self, x, n):
            if n == 0:
                return 1
    
            temp = self.pow(x, n / 2)
            if n % 2 == 0:
                return temp * temp
            else:
                return temp * temp * x

Log in to reply
 

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