40ms python recursive log(n) solution


  • 0
    Z
    class Solution(object):
        def myPow(self, x, n):
            """
            :type x: float; n: int;; rtype: float
            """
            # return x**n
            
            if n == 0 :                return 1
            elif x in [0, 1] or n == 1:
                return x
            elif n == -1:
                return 1 / x
    
            half = self.myPow(x, n // 2 if n > 0 else (n+1) //2 )
    
            if n%2 == 0:
                return half * half
            elif n > 0:
                return half * half * x
            else:
                return half * half / x
    

    #40ms
    #87.28%


Log in to reply
 

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