Implement pow(x, n), which calculates x raised to the power n (xn).

Example 1:


Input: 2.00000, 10
Output: 1024.00000


Example 2:


Input: 2.10000, 3
Output: 9.26100


Example 3:


Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25


Note:


       -100.0 < x < 100.0
       n is a 32-bit signed integer, within the range [−231, 231 − 1]

Difficulty: interview

Starter code:

class Solution:
    def myPow(self, x: float, n: int) -> float:
        

Solutions:
class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n == 0:
             return 1
         if abs(n) == 1:
             if n == 1:
                 return x
             else:
                 return 1/x
         if n > 0:
             a, b = int(n//2), n%2
         else:
             a, b = -int(-n//2), -(n%2)
         y = self.myPow(x, a)
         z = self.myPow(x, b)
         return y*y*z

class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n < 0:
             return 1 / self.myPow(x, -n)
         elif n == 1:
             return x
         elif n == 0:
             return 1
         elif n % 2:
             return x * self.myPow(x, n - 1)
         else:
             t = self.myPow(x, n // 2)
             return t * t


class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n < 0:
             return 1 / self.myPow(x, -n)
         elif n == 1:
             return x
         elif n == 0:
             return 1
         elif n % 2:
             return x * self.myPow(x, n - 1)
         else:
             t = self.myPow(x, n // 2)
             return t * t


class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if 0 <= n < 2:
             return [1, x][n]
         if n < 0:
             n, x = -n, 1 / x
         temp = divmod(n, 2)
         return self.myPow(x, temp[0]) ** 2 * self.myPow(x, temp[1])

class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n == 0:
             return 1
         if n == 1:
             return x
         if n < 0:
             return self.myPow(1/x, -n)
         if (n % 2) == 0:
             return self.myPow(x*x, n/2) 
         else:
             return self.myPow(x*x, (n-1)/2) * x

class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         return pow(x,n)

class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n == 0:
             return 1
         if n < 0:
             n = -n
             x = 1/x
         if n % 2 == 0:
             return self.myPow(x*x,n/2)
         else:
             return self.myPow(x*x,(n-1)/2)*x

class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n<0:return 1/self.myPow(x,-n)
         elif n==0:return 1
         elif n>1000:return self.myPow(self.myPow(x,1000),n//1000)*self.myPow(x,n%1000)
         else: return x*self.myPow(x,n-1)

class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         return x**n

class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n < 0:
             return 1 / self.myPow(x, -n)
         elif n == 1:
             return x
         elif n == 0:
             return 1
         elif n % 2:
             return x * self.myPow(x, n - 1)
         else:
             t = self.myPow(x, n // 2)
             return t * t


class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         output=1
         power=0
         power_applied=1
         product=x
         m=abs(n)
         while m!=0 and power!=m:
             if (power+power_applied*2)<m:
                 product=product*product
                 power_applied=power_applied*2
                 power=power+power_applied
             else:
                 product=x
                 power_applied=1
                 power=power+power_applied
             output=output*product
         if n<0: output=1/output
         return output

class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if not n:
             return 1
         if n < 0:
             return 1 / self.myPow(x, -n)
         if n % 2:
             return x * self.myPow(x, n-1)
         return self.myPow(x*x, n/2)


class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         return x ** n


class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n == 0 :
             return 1.00000
         if n < 0 :
             return 1 / self.myPow(x , -n)
         
         if n % 2 != 0 :
             return x * self.myPow(x , n - 1)
         return self.myPow(x * x , n / 2)
         


class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n < 0:
             return 1 / self.powRecu(x, -n)
         
         return self.powRecu(x, n)
     
     def powRecu(self, x, n):
         if n == 0:
             return 1.0
         
         if n % 2 == 0:
             return self.powRecu(x * x, n // 2)
         else:
             return x * self.powRecu(x * x, n // 2)

class Solution:
     
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         if n == 0: return 1
         if n<0:
             n= -n
             x = 1/x
         res = 1
         
         while n>0:
             cnt = 1
             mul = x
             while (cnt+cnt) <= n:
                 mul = mul * mul
                 cnt += cnt
             n = n - cnt
             res = mul * res
         if n%2 == 1: res = res*x
         
         return res

class Solution:
     def myPow(self, x, n):
         """
         :type x: float
         :type n: int
         :rtype: float
         """
         return x**n